Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'removeNoteWithConfirmation' command and associate with 'remove' UI #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
}, {
"command": "linenote.removeNote",
"title": "%command.removeNote.title%"
}, {
"command": "linenote.removeNoteWithConfirmation",
"title": "%command.removeNoteWithConfirmation.title%"
}],
"menus": {
"editor/context": [{
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"command.addNote.title": "Add note at current position",
"command.openNote.title": "Open notes at current position",
"command.removeNote.title": "Remove the note at current position",
"command.removeNoteWithConfirmation.title": "Remove the note at current position, but confirm first",
"configuration.lineColor.description": "Background color of line with notes (name or HEX)",
"configuration.rulerColor.description": "Color of the ruler with notes (name or HEX)",
"configuration.automaticallyDelete.description": "If it set to true, notes that do not correspond to files are automatically deleted.",
Expand Down
55 changes: 36 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ export const activate = (context: vscode.ExtensionContext) => {
];
};

const removeNoteImmediately = async (notePath?: string) => {
const editor = vscode.window.activeTextEditor;
if (notePath) {
// remove specified note (when invoked from the hover text)
const note = await Note.fromNotePath(notePath);
await note.remove();
decorateDebounce();
} else if (editor) {
// remove one note at current cursor (when invoked from the command palette)
const fsPath = editor.document.uri.fsPath;
if (await isNotePath(fsPath)) {
return;
}
const notes = await getCorrespondingNotes(fsPath);
const [from, to] = getSelectionLineRange(editor);
const note = notes.find(note => note.isOverlapped(from, to));
if (note) {
await note.remove();
decorateDebounce();
}
}
};

context.subscriptions.push(
new vscode.Disposable(() => (disposed = true)),

Expand Down Expand Up @@ -164,27 +187,21 @@ export const activate = (context: vscode.ExtensionContext) => {

vscode.commands.registerCommand(
"linenote.removeNote",
removeNoteImmediately
),

vscode.commands.registerCommand(
"linenote.removeNoteWithConfirmation",
async (notePath?: string) => {
const editor = vscode.window.activeTextEditor;
if (notePath) {
// remove specified note (when invoked from the hover text)
const note = await Note.fromNotePath(notePath);
await note.remove();
decorateDebounce();
} else if (editor) {
// remove one note at current cursor (when invoked from the command palette)
const fsPath = editor.document.uri.fsPath;
if (await isNotePath(fsPath)) {
return;
}
const notes = await getCorrespondingNotes(fsPath);
const [from, to] = getSelectionLineRange(editor);
const note = notes.find(note => note.isOverlapped(from, to));
if (note) {
await note.remove();
decorateDebounce();
}
const confirmed = await vscode.window
.showInformationMessage(`Are you sure you'd like to permanently remove this note?`,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not as familiar with translation files... maybe add a translation for this? How?

...['Yes', 'No']) === 'Yes';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for Yes/No, probably.


if (!confirmed) {
return;
}

return await removeNoteImmediately(notePath);
}
),

Expand Down
4 changes: 2 additions & 2 deletions src/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ export class Note implements Props {
)}`
)})`;
const remove = `[Remove](${vscode.Uri.parse(
`command:linenote.removeNote?${encodeURIComponent(
`command:linenote.removeNoteWithConfirmation?${encodeURIComponent(
JSON.stringify(this.notePath)
)}`
)})`;

return `${body}\n\n*${path.basename(this.notePath)}* ${edit} ${remove}`;
return `${body}\n\n*${path.basename(this.notePath)}* | ${edit} | ${remove}`;
}

// remove empty dir recursively
Expand Down