Skip to content

Commit 02a35b6

Browse files
committed
Lexical: Added new WYSIWYG to chapter/book/shelf descriptions
1 parent b80992c commit 02a35b6

File tree

8 files changed

+54
-84
lines changed

8 files changed

+54
-84
lines changed

resources/js/components/wysiwyg-input.js

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Component} from './component';
2+
import {el} from "../wysiwyg/utils/dom";
3+
import {SimpleWysiwygEditorInterface} from "../wysiwyg";
4+
5+
export class WysiwygInput extends Component {
6+
private elem!: HTMLTextAreaElement;
7+
private wysiwygEditor!: SimpleWysiwygEditorInterface;
8+
private textDirection!: string;
9+
10+
async setup() {
11+
this.elem = this.$el as HTMLTextAreaElement;
12+
this.textDirection = this.$opts.textDirection;
13+
14+
type WysiwygModule = typeof import('../wysiwyg');
15+
const wysiwygModule = (await window.importVersioned('wysiwyg')) as WysiwygModule;
16+
const container = el('div', {class: 'comment-editor-container'});
17+
this.elem.parentElement?.appendChild(container);
18+
this.elem.hidden = true;
19+
20+
this.wysiwygEditor = wysiwygModule.createBasicEditorInstance(container as HTMLElement, this.elem.value, {
21+
darkMode: document.documentElement.classList.contains('dark-mode'),
22+
textDirection: this.textDirection,
23+
translations: (window as unknown as Record<string, Object>).editor_translations,
24+
});
25+
26+
this.wysiwygEditor.onChange(() => {
27+
this.wysiwygEditor.getContentAsHtml().then(html => {
28+
this.elem.value = html;
29+
});
30+
});
31+
}
32+
}

resources/js/wysiwyg-tinymce/config.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -310,54 +310,6 @@ export function buildForEditor(options) {
310310
};
311311
}
312312

313-
/**
314-
* @param {WysiwygConfigOptions} options
315-
* @return {RawEditorOptions}
316-
*/
317-
export function buildForInput(options) {
318-
// Set language
319-
window.tinymce.addI18n(options.language, options.translationMap);
320-
321-
// BookStack Version
322-
const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
323-
324-
// Return config object
325-
return {
326-
width: '100%',
327-
height: '185px',
328-
target: options.containerElement,
329-
cache_suffix: `?version=${version}`,
330-
content_css: [
331-
window.baseUrl('/dist/styles.css'),
332-
],
333-
branding: false,
334-
skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
335-
body_class: 'wysiwyg-input',
336-
browser_spellcheck: true,
337-
relative_urls: false,
338-
language: options.language,
339-
directionality: options.textDirection,
340-
remove_script_host: false,
341-
document_base_url: window.baseUrl('/'),
342-
end_container_on_empty_block: true,
343-
remove_trailing_brs: false,
344-
statusbar: false,
345-
menubar: false,
346-
plugins: 'link autolink lists',
347-
contextmenu: false,
348-
toolbar: 'bold italic link bullist numlist',
349-
content_style: getContentStyle(options),
350-
file_picker_types: 'file',
351-
valid_elements: 'p,a[href|title|target],ol,ul,li,strong,em,br',
352-
file_picker_callback: filePickerCallback,
353-
init_instance_callback(editor) {
354-
addCustomHeadContent(editor.getDoc());
355-
356-
editor.contentDocument.documentElement.classList.toggle('dark-mode', options.darkMode);
357-
},
358-
};
359-
}
360-
361313
/**
362314
* @typedef {Object} WysiwygConfigOptions
363315
* @property {Element} containerElement

resources/js/wysiwyg/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export function createBasicEditorInstance(container: HTMLElement, htmlContent: s
123123

124124
export class SimpleWysiwygEditorInterface {
125125
protected context: EditorUiContext;
126+
protected onChangeListeners: (() => void)[] = [];
127+
protected editorListenerTeardown: (() => void)|null = null;
126128

127129
constructor(context: EditorUiContext) {
128130
this.context = context;
@@ -132,12 +134,32 @@ export class SimpleWysiwygEditorInterface {
132134
return await getEditorContentAsHtml(this.context.editor);
133135
}
134136

137+
onChange(listener: () => void) {
138+
this.onChangeListeners.push(listener);
139+
this.startListeningToChanges();
140+
}
141+
135142
focus(): void {
136143
focusEditor(this.context.editor);
137144
}
138145

139146
remove() {
140147
this.context.editorDOM.remove();
141148
this.context.manager.teardown();
149+
if (this.editorListenerTeardown) {
150+
this.editorListenerTeardown();
151+
}
152+
}
153+
154+
protected startListeningToChanges(): void {
155+
if (this.editorListenerTeardown) {
156+
return;
157+
}
158+
159+
this.editorListenerTeardown = this.context.editor.registerUpdateListener(() => {
160+
for (const listener of this.onChangeListeners) {
161+
listener();
162+
}
163+
});
142164
}
143165
}

resources/views/books/parts/form.blade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
@push('head')
2-
<script src="{{ versioned_asset('libs/tinymce/tinymce.min.js') }}" nonce="{{ $cspNonce }}"></script>
3-
@endpush
4-
51
{{ csrf_field() }}
62
<div class="form-group title-input">
73
<label for="name">{{ trans('common.name') }}</label>

resources/views/chapters/parts/form.blade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
@push('head')
2-
<script src="{{ versioned_asset('libs/tinymce/tinymce.min.js') }}" nonce="{{ $cspNonce }}"></script>
3-
@endpush
4-
51
{{ csrf_field() }}
62
<div class="form-group title-input">
73
<label for="name">{{ trans('common.name') }}</label>

resources/views/form/description-html-input.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<textarea component="wysiwyg-input"
2-
option:wysiwyg-input:language="{{ $locale->htmlLang() }}"
32
option:wysiwyg-input:text-direction="{{ $locale->htmlDirection() }}"
43
id="description_html" name="description_html" rows="5"
54
@if($errors->has('description_html')) class="text-neg" @endif>@if(isset($model) || old('description_html')){{ old('description_html') ?? $model->descriptionHtml()}}@endif</textarea>

resources/views/shelves/parts/form.blade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
@push('head')
2-
<script src="{{ versioned_asset('libs/tinymce/tinymce.min.js') }}" nonce="{{ $cspNonce }}"></script>
3-
@endpush
4-
51
{{ csrf_field() }}
62
<div class="form-group title-input">
73
<label for="name">{{ trans('common.name') }}</label>

0 commit comments

Comments
 (0)