]> BookStack Code Mirror - bookstack/commitdiff
Editors: Updated entity link select to pre-fill with selection
authorDan Brown <redacted>
Sun, 24 Sep 2023 17:33:33 +0000 (18:33 +0100)
committerDan Brown <redacted>
Sun, 24 Sep 2023 17:33:33 +0000 (18:33 +0100)
Updated all uses across both editors, so the entity link selector popup
now initates a search with the selection text if existing.

For #4571

resources/js/components/entity-selector-popup.js
resources/js/components/entity-selector.js
resources/js/markdown/actions.js
resources/js/wysiwyg/config.js
resources/js/wysiwyg/shortcuts.js

index e21e67fb33ebb2b6e1ec914d30d425114857bce4..9ff67d53efbe49637ddabd87f9fc50f333e9f2ac 100644 (file)
@@ -15,9 +15,14 @@ export class EntitySelectorPopup extends Component {
         window.$events.listen('entity-select-confirm', this.handleConfirmedSelection.bind(this));
     }
 
-    show(callback) {
+    show(callback, searchText = '') {
         this.callback = callback;
         this.getPopup().show();
+
+        if (searchText) {
+            this.getSelector().searchText(searchText);
+        }
+
         this.getSelector().focusSearch();
     }
 
index f12108fbb497877d9d96cd345e66cdf6a24f51bf..9cda35874019e4291951712de8ab96e719c454ec 100644 (file)
@@ -87,6 +87,11 @@ export class EntitySelector extends Component {
         this.searchInput.focus();
     }
 
+    searchText(queryText) {
+        this.searchInput.value = queryText;
+        this.searchEntities(queryText);
+    }
+
     showLoading() {
         this.loading.style.display = 'block';
         this.resultsContainer.style.display = 'none';
index 3f9df47788f0c7b6cb70092310e740e4d4791cfa..a7fde9322d61b2b309e18b0015e80bafb2469869 100644 (file)
@@ -68,11 +68,12 @@ export class Actions {
 
         /** @type {EntitySelectorPopup} * */
         const selector = window.$components.first('entity-selector-popup');
+        const selectionText = this.#getSelectionText(selectionRange);
         selector.show(entity => {
-            const selectedText = this.#getSelectionText(selectionRange) || entity.name;
+            const selectedText = selectionText || entity.name;
             const newText = `[${selectedText}](${entity.link})`;
             this.#replaceSelection(newText, newText.length, selectionRange);
-        });
+        }, selectionText);
     }
 
     // Show draw.io if enabled and handle save.
index d93c9644e5db99b7e77af8bdb0194b14adc516d1..984081bd602f2c7efc1ae3c175008a9bec3b63da 100644 (file)
@@ -78,12 +78,13 @@ function filePickerCallback(callback, value, meta) {
     if (meta.filetype === 'file') {
         /** @type {EntitySelectorPopup} * */
         const selector = window.$components.first('entity-selector-popup');
+        const selectionText = this.selection.getContent({format: 'text'}).trim();
         selector.show(entity => {
             callback(entity.link, {
                 text: entity.name,
                 title: entity.name,
             });
-        });
+        }, selectionText);
     }
 
     if (meta.filetype === 'image') {
index 1c20df9c5516c8df6a640151b0b5be4c1b99fa11..147e3c2d5c9aceb2d4c573a6aef5d8e4f74335c7 100644 (file)
@@ -48,6 +48,7 @@ export function register(editor) {
     editor.shortcuts.add('meta+shift+K', '', () => {
         /** @var {EntitySelectorPopup} * */
         const selectorPopup = window.$components.first('entity-selector-popup');
+        const selectionText = editor.selection.getContent({format: 'text'}).trim();
         selectorPopup.show(entity => {
             if (editor.selection.isCollapsed()) {
                 editor.insertContent(editor.dom.createHTML('a', {href: entity.link}, editor.dom.encode(entity.name)));
@@ -57,6 +58,6 @@ export function register(editor) {
 
             editor.selection.collapse(false);
             editor.focus();
-        });
+        }, selectionText);
     });
 }