Haven't really worked with TinyMCE before, but since the options are under the init prop, my guess is that these options are not reactive because they're initial values. After looking at the source code, I haven't seen anything suggesting they're supposed to be reactive. However, I found that they expose a rerender method on the component. So I explored with that in a Vue sandbox and it turns out explicitely calling the rerender method works, but with one downside (and possibly others I haven't seen), which might be negligeable and/or fixable, but I didn't want to spend a ton of time on it since it's just a proof of concept. The downside in question is that since the component needs to be rerendered, it loses its current state, which means that the current editor content will still remain intact (because of the two-way binding), but the undo/redo history is completely wiped. Here's an adjusted code snipped of my explorations:
<template>
<button @click="darkMode = !darkMode">Toggle light/dark mode</button>
<Editor
ref="editor"
api-key="api-key"
:init="initOptions"
:modelValue="modelValue"
@update:modelValue="(val) => emit('update:modelValue', val)"
/>
</template>
<script setup>
import Editor from "@tinymce/tinymce-vue";
import { computed, onMounted, ref, reactive, watch } from "vue";
defineProps({
modelValue: String,
});
const emit = defineEmits(["update:modelValue", "save"]);
const editor = ref();
const darkMode = ref(true);
const theme = computed(() => {
return darkMode.value
? {
skin: "oxide-dark",
content_css: "dark",
}
: {
skin: "oxide",
content_css: "default",
};
});
watch(darkMode, () => {
editor.value.rerender(initOptions.value);
});
const initOptions = computed(() => {
return {
height: 750,
/* some plugins and toolbar options */
...theme.value,
save_onsavecallback: () => emit("save"),
};
});
</script>
What I added/adjusted:
- Replaced
reactiveby acomputedforinitOptions(since it depends ontheme) - Added a
watchon thedarkModevalue which will cause a the editor to rerender with the newinitOptions - Added a button to toggle the
darkModeand changed thedarkModevariable to areffor testing purposes, but you can revert that back to what you had before