|
1 | 1 | define(function (require, exports, module) { |
2 | | - const EditorManager = require("editor/EditorManager"), |
3 | | - Editor = require("editor/Editor").Editor; |
| 2 | + const EditorManager = require("editor/EditorManager"); |
| 3 | + const Editor = require("editor/Editor").Editor; |
| 4 | + |
| 5 | + const Helper = require("./helper"); |
4 | 6 |
|
5 | 7 | const GUTTER_NAME = "CodeMirror-bookmarkGutter", |
6 | 8 | BOOKMARK_PRIORITY = 100; |
7 | 9 |
|
8 | | - // the bookmark svg icon |
9 | | - const bookmarkSvg = require("text!styles/images/bookmark.svg"); |
10 | | - |
11 | 10 | // initialize the bookmark gutter |
12 | 11 | Editor.registerGutter(GUTTER_NAME, BOOKMARK_PRIORITY); |
13 | 12 |
|
14 | 13 | /** |
15 | | - * This function is responsible to toggle a bookmark at the current cursor position |
| 14 | + * This function toggles a bookmark on a specific line |
| 15 | + * |
| 16 | + * @param {Editor} editor - The current editor instance |
| 17 | + * @param {number} line - The line number to toggle bookmark on |
| 18 | + */ |
| 19 | + function toggleLineBookmark(editor, line) { |
| 20 | + if (Helper.hasBookmark(editor, line, GUTTER_NAME)) { |
| 21 | + editor.setGutterMarker(line, GUTTER_NAME, ""); |
| 22 | + } else { |
| 23 | + editor.setGutterMarker(line, GUTTER_NAME, Helper.createBookmarkMarker()); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * This function is responsible to toggle bookmarks at the current cursor position(s) |
16 | 29 | */ |
17 | 30 | function toggleBookmark() { |
18 | 31 | const editor = EditorManager.getFocusedEditor(); |
19 | 32 | if (!editor) { |
20 | 33 | return; |
21 | 34 | } |
22 | 35 |
|
23 | | - const selection = editor.getSelection(); |
24 | | - const currentLine = selection.start.line; |
25 | | - |
26 | | - // check whether there's already a bookmark on this line |
27 | | - const marker = editor.getGutterMarker(currentLine, GUTTER_NAME); |
| 36 | + const selections = editor.getSelections(); |
| 37 | + const uniqueLines = Helper.getUniqueLines(selections); |
28 | 38 |
|
29 | | - if (marker) { |
30 | | - // remove bookmark if exists |
31 | | - editor.setGutterMarker(currentLine, GUTTER_NAME, ""); |
32 | | - } else { |
33 | | - // add the bookmark |
34 | | - const $marker = $("<div>").addClass("bookmark-icon").html(bookmarkSvg); |
35 | | - editor.setGutterMarker(currentLine, GUTTER_NAME, $marker[0]); |
36 | | - } |
| 39 | + // process each unique line |
| 40 | + uniqueLines.forEach((line) => { |
| 41 | + toggleLineBookmark(editor, line); |
| 42 | + }); |
37 | 43 | } |
38 | 44 |
|
39 | 45 | exports.toggleBookmark = toggleBookmark; |
|
0 commit comments