Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Add editorThemeCompartment for customizing editor theme
Browse files Browse the repository at this point in the history
  • Loading branch information
darling committed Dec 28, 2023
1 parent f2ef385 commit 4b56e62
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions lib/components/base/MarkdownEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ const props = withDefaults(
const editorRef = ref<HTMLDivElement>()
let editor: EditorView | null = null
let isDisabledCompartment: Compartment | null = null
let editorThemeCompartment: Compartment | null = null
const emit = defineEmits(['update:modelValue'])
Expand All @@ -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': {
Expand Down Expand Up @@ -430,7 +433,6 @@ onMounted(() => {
const editorState = EditorState.create({
extensions: [
theme,
eventHandlers,
updateListener,
keymap.of([indentWithTab]),
Expand All @@ -443,6 +445,7 @@ onMounted(() => {
cm_placeholder(props.placeholder || ''),
inputFilter,
isDisabledCompartment.of(disabledCompartment),
editorThemeCompartment.of(theme),
],
})
Expand Down Expand Up @@ -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,
}
)
Expand Down Expand Up @@ -905,4 +943,10 @@ function openVideoModal() {
justify-content: start;
}
}
.cm-disabled {
opacity: 0.6;
pointer-events: none;
cursor: not-allowed;
}
</style>

0 comments on commit 4b56e62

Please sign in to comment.