Skip to content

Commit fd26f54

Browse files
authored
Merge pull request #3298 from BookStackApp/wysiwyg_links
WYSIWYG editor link updates
2 parents 77ad819 + 6252b46 commit fd26f54

File tree

5 files changed

+92
-48
lines changed

5 files changed

+92
-48
lines changed

resources/js/wysiwyg/config.js

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {register as registerShortcuts} from "./shortcuts";
22
import {listen as listenForCommonEvents} from "./common-events";
33
import {scrollToQueryString} from "./scrolling";
44
import {listenForDragAndPaste} from "./drop-paste-handling";
5+
import {getPrimaryToolbar, registerAdditionalToolbars} from "./toolbars";
56

67
import {getPlugin as getCodeeditorPlugin} from "./plugin-codeeditor";
78
import {getPlugin as getDrawioPlugin} from "./plugin-drawio";
@@ -58,48 +59,6 @@ function file_picker_callback(callback, value, meta) {
5859

5960
}
6061

61-
/**
62-
* @param {WysiwygConfigOptions} options
63-
* @return {{toolbar: string, groupButtons: Object<string, Object>}}
64-
*/
65-
function buildToolbar(options) {
66-
const textDirPlugins = options.textDirection === 'rtl' ? 'ltr rtl' : '';
67-
68-
const groupButtons = {
69-
formatoverflow: {
70-
icon: 'more-drawer',
71-
tooltip: 'More',
72-
items: 'strikethrough superscript subscript inlinecode removeformat'
73-
},
74-
listoverflow: {
75-
icon: 'more-drawer',
76-
tooltip: 'More',
77-
items: 'outdent indent'
78-
},
79-
insertoverflow: {
80-
icon: 'more-drawer',
81-
tooltip: 'More',
82-
items: 'hr codeeditor drawio media details'
83-
}
84-
};
85-
86-
const toolbar = [
87-
'undo redo',
88-
'styleselect',
89-
'bold italic underline forecolor backcolor formatoverflow',
90-
'alignleft aligncenter alignright alignjustify',
91-
'bullist numlist listoverflow',
92-
textDirPlugins,
93-
'link table imagemanager-insert insertoverflow',
94-
'code about fullscreen'
95-
];
96-
97-
return {
98-
toolbar: toolbar.filter(row => Boolean(row)).join(' | '),
99-
groupButtons,
100-
};
101-
}
102-
10362
/**
10463
* @param {WysiwygConfigOptions} options
10564
* @return {string}
@@ -218,8 +177,6 @@ export function build(options) {
218177

219178
// Set language
220179
window.tinymce.addI18n(options.language, options.translationMap);
221-
// Build toolbar content
222-
const {toolbar, groupButtons: toolBarGroupButtons} = buildToolbar(options);
223180

224181
// BookStack Version
225182
const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
@@ -261,7 +218,7 @@ export function build(options) {
261218
plugins: gatherPlugins(options),
262219
imagetools_toolbar: 'imageoptions',
263220
contextmenu: false,
264-
toolbar: toolbar,
221+
toolbar: getPrimaryToolbar(options),
265222
content_style: getContentStyle(options),
266223
style_formats,
267224
style_formats_merge: false,
@@ -281,9 +238,7 @@ export function build(options) {
281238
head.innerHTML += fetchCustomHeadContent();
282239
},
283240
setup(editor) {
284-
for (const [key, config] of Object.entries(toolBarGroupButtons)) {
285-
editor.ui.registry.addGroupToolbarButton(key, config);
286-
}
241+
registerAdditionalToolbars(editor, options);
287242
getSetupCallback(options)(editor);
288243
},
289244
};

resources/js/wysiwyg/shortcuts.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ export function register(editor) {
3939

4040
editor.formatter.apply('callout' + newFormat);
4141
});
42+
43+
// Link selector shortcut
44+
editor.shortcuts.add('meta+shift+K', '', function() {
45+
window.EntitySelectorPopup.show(function(entity) {
46+
47+
if (editor.selection.isCollapsed()) {
48+
editor.insertContent(editor.dom.createHTML('a', {href: entity.link}, editor.dom.encode(entity.name)));
49+
} else {
50+
editor.formatter.apply('link', {href: entity.link});
51+
}
52+
53+
editor.selection.collapse(false);
54+
editor.focus();
55+
})
56+
});
4257
}

resources/js/wysiwyg/toolbars.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @param {WysiwygConfigOptions} options
3+
* @return {String}
4+
*/
5+
export function getPrimaryToolbar(options) {
6+
const textDirPlugins = options.textDirection === 'rtl' ? 'ltr rtl' : '';
7+
8+
const toolbar = [
9+
'undo redo',
10+
'styleselect',
11+
'bold italic underline forecolor backcolor formatoverflow',
12+
'alignleft aligncenter alignright alignjustify',
13+
'bullist numlist listoverflow',
14+
textDirPlugins,
15+
'link table imagemanager-insert insertoverflow',
16+
'code about fullscreen'
17+
];
18+
19+
return toolbar.filter(row => Boolean(row)).join(' | ');
20+
}
21+
22+
/**
23+
* @param {Editor} editor
24+
*/
25+
function registerPrimaryToolbarGroups(editor) {
26+
editor.ui.registry.addGroupToolbarButton('formatoverflow', {
27+
icon: 'more-drawer',
28+
tooltip: 'More',
29+
items: 'strikethrough superscript subscript inlinecode removeformat'
30+
});
31+
editor.ui.registry.addGroupToolbarButton('listoverflow', {
32+
icon: 'more-drawer',
33+
tooltip: 'More',
34+
items: 'outdent indent'
35+
});
36+
editor.ui.registry.addGroupToolbarButton('insertoverflow', {
37+
icon: 'more-drawer',
38+
tooltip: 'More',
39+
items: 'hr codeeditor drawio media details'
40+
});
41+
}
42+
43+
/**
44+
* @param {Editor} editor
45+
*/
46+
function registerLinkContextToolbar(editor) {
47+
editor.ui.registry.addContextToolbar('linkcontexttoolbar', {
48+
predicate(node) {
49+
return node.closest('a') !== null;
50+
},
51+
position: 'node',
52+
scope: 'node',
53+
items: 'link unlink openlink'
54+
});
55+
}
56+
57+
/**
58+
* @param {Editor} editor
59+
* @param {WysiwygConfigOptions} options
60+
*/
61+
export function registerAdditionalToolbars(editor, options) {
62+
registerPrimaryToolbarGroups(editor);
63+
registerLinkContextToolbar(editor);
64+
}

resources/lang/en/editor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
'editor_tiny_license_link' => 'The copyright and license details of TinyMCE can be found here.',
146146
'save_continue' => 'Save Page & Continue',
147147
'callouts_cycle' => '(Keep pressing to toggle through types)',
148+
'link_selector' => 'Link to content',
148149
'shortcuts' => 'Shortcuts',
149150
'shortcut' => 'Shortcut',
150151
'shortcuts_intro' => 'The following shortcuts are available in the editor:',

resources/views/help/wysiwyg.blade.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@
115115
{{ trans('editor.callouts_cycle') }}
116116
</td>
117117
</tr>
118+
<tr>
119+
<td>
120+
<code>Ctrl</code>+<code>Shift</code>+<code>K</code>
121+
</td>
122+
<td>
123+
<code>Cmd</code>+<code>Shift</code>+<code>K</code>
124+
</td>
125+
<td>{{ trans('editor.link_selector') }}</td>
126+
</tr>
118127
</tbody>
119128
</table>
120129

0 commit comments

Comments
 (0)