Skip to content

Commit e274a5f

Browse files
authored
Merge pull request #6100 from BookStackApp/wysiwyg_minimal_inline_code
WYSIWYG: Added inline code support to minimal editor
2 parents 0760e67 + 18364d1 commit e274a5f

5 files changed

Lines changed: 40 additions & 31 deletions

File tree

app/Util/HtmlDescriptionFilter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class HtmlDescriptionFilter
2727
'span' => [],
2828
'em' => [],
2929
'br' => [],
30+
'code' => [],
3031
];
3132

3233
public static function filterFromString(string $html): string

resources/js/wysiwyg/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
5959
mergeRegister(
6060
registerRichText(editor),
6161
registerHistory(editor, createEmptyHistoryState(), 300),
62-
registerShortcuts(context),
62+
registerShortcuts(context, true),
6363
registerKeyboardHandling(context),
6464
registerMouseHandling(context),
6565
registerSelectionHandling(context),
@@ -123,7 +123,7 @@ export function createBasicEditorInstance(container: HTMLElement, htmlContent: s
123123
const editorTeardown = mergeRegister(
124124
registerRichText(editor),
125125
registerHistory(editor, createEmptyHistoryState(), 300),
126-
registerShortcuts(context),
126+
registerShortcuts(context, false),
127127
registerAutoLinks(editor),
128128
);
129129

@@ -157,7 +157,7 @@ export function createCommentEditorInstance(container: HTMLElement, htmlContent:
157157
const editorTeardown = mergeRegister(
158158
registerRichText(editor),
159159
registerHistory(editor, createEmptyHistoryState(), 300),
160-
registerShortcuts(context),
160+
registerShortcuts(context, false),
161161
registerAutoLinks(editor),
162162
registerMentions(context),
163163
);

resources/js/wysiwyg/services/shortcuts.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,9 @@ type ShortcutAction = (editor: LexicalEditor, context: EditorUiContext) => boole
3838
* List of action functions by their shortcut combo.
3939
* We use "meta" as an abstraction for ctrl/cmd depending on platform.
4040
*/
41-
const actionsByKeys: Record<string, ShortcutAction> = {
42-
'meta+s': () => {
43-
window.$events.emit('editor-save-draft');
44-
return true;
45-
},
46-
'meta+enter': () => {
47-
window.$events.emit('editor-save-page');
48-
return true;
49-
},
50-
'meta+1': (editor, context) => headerHandler(context, 'h2'),
51-
'meta+2': (editor, context) => headerHandler(context, 'h3'),
52-
'meta+3': (editor, context) => headerHandler(context, 'h4'),
53-
'meta+4': (editor, context) => headerHandler(context, 'h5'),
54-
'meta+5': wrapFormatAction(toggleSelectionAsParagraph),
55-
'meta+d': wrapFormatAction(toggleSelectionAsParagraph),
56-
'meta+6': wrapFormatAction(toggleSelectionAsBlockquote),
57-
'meta+q': wrapFormatAction(toggleSelectionAsBlockquote),
58-
'meta+7': wrapFormatAction(formatCodeBlock),
59-
'meta+e': wrapFormatAction(formatCodeBlock),
41+
const baseActionsByKeys: Record<string, ShortcutAction> = {
6042
'meta+8': toggleInlineCode,
6143
'meta+shift+e': toggleInlineCode,
62-
'meta+9': wrapFormatAction(cycleSelectionCalloutFormats),
63-
6444
'meta+o': wrapFormatAction((e) => toggleSelectionAsList(e, 'number')),
6545
'meta+p': wrapFormatAction((e) => toggleSelectionAsList(e, 'bullet')),
6646
'meta+k': (editor, context) => {
@@ -87,12 +67,39 @@ const actionsByKeys: Record<string, ShortcutAction> = {
8767
},
8868
};
8969

90-
function createKeyDownListener(context: EditorUiContext): (e: KeyboardEvent) => void {
70+
/**
71+
* An extended set of the above, used for fuller-featured editors with heavier block-level formatting.
72+
*/
73+
const extendedActionsByKeys: Record<string, ShortcutAction> = {
74+
...baseActionsByKeys,
75+
'meta+s': () => {
76+
window.$events.emit('editor-save-draft');
77+
return true;
78+
},
79+
'meta+enter': () => {
80+
window.$events.emit('editor-save-page');
81+
return true;
82+
},
83+
'meta+1': (editor, context) => headerHandler(context, 'h2'),
84+
'meta+2': (editor, context) => headerHandler(context, 'h3'),
85+
'meta+3': (editor, context) => headerHandler(context, 'h4'),
86+
'meta+4': (editor, context) => headerHandler(context, 'h5'),
87+
'meta+5': wrapFormatAction(toggleSelectionAsParagraph),
88+
'meta+d': wrapFormatAction(toggleSelectionAsParagraph),
89+
'meta+6': wrapFormatAction(toggleSelectionAsBlockquote),
90+
'meta+7': wrapFormatAction(formatCodeBlock),
91+
'meta+e': wrapFormatAction(formatCodeBlock),
92+
'meta+q': wrapFormatAction(toggleSelectionAsBlockquote),
93+
'meta+9': wrapFormatAction(cycleSelectionCalloutFormats),
94+
};
95+
96+
function createKeyDownListener(context: EditorUiContext, useExtended: boolean): (e: KeyboardEvent) => void {
97+
const keySetToUse = useExtended ? extendedActionsByKeys : baseActionsByKeys;
9198
return (event: KeyboardEvent) => {
9299
const combo = keyboardEventToKeyComboString(event);
93100
// console.log(`pressed: ${combo}`);
94-
if (actionsByKeys[combo]) {
95-
const handled = actionsByKeys[combo](context.editor, context);
101+
if (keySetToUse[combo]) {
102+
const handled = keySetToUse[combo](context.editor, context);
96103
if (handled) {
97104
event.stopPropagation();
98105
event.preventDefault();
@@ -127,8 +134,8 @@ function overrideDefaultCommands(editor: LexicalEditor) {
127134
}, COMMAND_PRIORITY_HIGH);
128135
}
129136

130-
export function registerShortcuts(context: EditorUiContext) {
131-
const listener = createKeyDownListener(context);
137+
export function registerShortcuts(context: EditorUiContext, useExtended: boolean) {
138+
const listener = createKeyDownListener(context, useExtended);
132139
overrideDefaultCommands(context.editor);
133140

134141
return context.editor.registerRootListener((rootElement: null | HTMLElement, prevRootElement: null | HTMLElement) => {

resources/js/wysiwyg/ui/defaults/toolbars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export function getBasicEditorToolbar(context: EditorUiContext): EditorContainer
227227
new EditorButton(bold),
228228
new EditorButton(italic),
229229
new EditorButton(link),
230+
new EditorButton(code),
230231
new EditorButton(bulletList),
231232
new EditorButton(numberList),
232233
])

tests/Entity/BookTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ public function test_description_limited_to_specific_html()
256256
{
257257
$book = $this->entities->book();
258258

259-
$input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" target="_blank" data-a="b">a</a><section>Hello</section></p>';
260-
$expected = '<p>Content<a href="#cat" target="_blank">a</a></p>';
259+
$input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" target="_blank" data-a="b">a</a><section>Hello</section><code id="abc">code</code></p>';
260+
$expected = '<p>Content<a href="#cat" target="_blank">a</a><code>code</code></p>';
261261

262262
$this->asEditor()->put($book->getUrl(), [
263263
'name' => $book->name,

0 commit comments

Comments
 (0)