From 4b56e62041e9993a1230d1b2efbe21589bb1cdef Mon Sep 17 00:00:00 2001 From: Carter Date: Wed, 27 Dec 2023 18:06:05 -0800 Subject: [PATCH] Add editorThemeCompartment for customizing editor theme --- lib/components/base/MarkdownEditor.vue | 54 +++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/components/base/MarkdownEditor.vue b/lib/components/base/MarkdownEditor.vue index f8344a73..56953679 100644 --- a/lib/components/base/MarkdownEditor.vue +++ b/lib/components/base/MarkdownEditor.vue @@ -328,6 +328,7 @@ const props = withDefaults( const editorRef = ref() let editor: EditorView | null = null let isDisabledCompartment: Compartment | null = null +let editorThemeCompartment: Compartment | null = null const emit = defineEmits(['update:modelValue']) @@ -338,6 +339,8 @@ onMounted(() => { } }) + editorThemeCompartment = new Compartment() + const theme = EditorView.theme({ // in defaults.scss there's references to .cm-content and such to inherit global styles '.cm-content': { @@ -430,7 +433,6 @@ onMounted(() => { const editorState = EditorState.create({ extensions: [ - theme, eventHandlers, updateListener, keymap.of([indentWithTab]), @@ -443,6 +445,7 @@ onMounted(() => { cm_placeholder(props.placeholder || ''), inputFilter, isDisabledCompartment.of(disabledCompartment), + editorThemeCompartment.of(theme), ], }) @@ -559,11 +562,46 @@ const BUTTONS: ButtonGroupMap = { watch( () => props.disabled, (newValue) => { - if (editor && isDisabledCompartment) { - editor.dispatch({ - effects: isDisabledCompartment.reconfigure(EditorState.readOnly.of(newValue)), - }) + if (editor) { + if (isDisabledCompartment) { + editor.dispatch({ + effects: [isDisabledCompartment.reconfigure(EditorState.readOnly.of(newValue))], + }) + } + + if (editorThemeCompartment) { + editor.dispatch({ + effects: [ + editorThemeCompartment.reconfigure( + EditorView.theme({ + // in defaults.scss there's references to .cm-content and such to inherit global styles + '.cm-content': { + marginBlockEnd: '0.5rem', + padding: '0.5rem', + minHeight: '200px', + caretColor: 'var(--color-contrast)', + width: '100%', + overflowX: 'scroll', + maxHeight: props.maxHeight ? `${props.maxHeight}px` : 'unset', + overflowY: 'scroll', + + opacity: newValue ? 0.6 : 1, + pointerEvents: newValue ? 'none' : 'all', + cursor: newValue ? 'not-allowed' : 'auto', + }, + '.cm-scroller': { + height: '100%', + overflow: 'visible', + }, + }) + ), + ], + }) + } } + }, + { + immediate: true, } ) @@ -905,4 +943,10 @@ function openVideoModal() { justify-content: start; } } + +.cm-disabled { + opacity: 0.6; + pointer-events: none; + cursor: not-allowed; +}