Skip to content

Commit 832b161

Browse files
committed
Expose rerender method to change init object
1 parent 026a059 commit 832b161

File tree

6 files changed

+3243
-2480
lines changed

6 files changed

+3243
-2480
lines changed

package.json

+12-10
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@
4141
"@ephox/bedrock-server": "^11.3.1",
4242
"@ephox/katamari": "^7.2.1",
4343
"@ephox/sugar": "^7.1.3",
44-
"@storybook/addon-actions": "^6.4.0-alpha.9",
45-
"@storybook/addon-essentials": "^6.4.0-alpha.9",
46-
"@storybook/addon-links": "^6.4.0-alpha.9",
44+
"@storybook/addon-actions": "^6.4.18",
45+
"@storybook/addon-essentials": "^6.4.18",
46+
"@storybook/addon-links": "^6.4.18",
4747
"@storybook/addon-notes": "^5.3.21",
4848
"@storybook/storybook-deployer": "^2.8.10",
49-
"@storybook/vue3": "^6.4.0-alpha.9",
49+
"@storybook/vue3": "^6.4.18",
5050
"@tinymce/beehive-flow": "^0.17.0",
5151
"@tinymce/eslint-plugin": "^1.9.1",
5252
"@tinymce/miniature": "^3.2.0",
5353
"@types/node": "^16.7.1",
54-
"@vue/compiler-sfc": "^3.0.0",
55-
"babel-loader": "^8.0.5",
54+
"@vitejs/plugin-vue": "^2.1.0",
55+
"@vue/compiler-sfc": "^3.2.29",
56+
"babel-loader": "^8.2.3",
5657
"babel-preset-vue": "^2.0.2",
5758
"babel-register": "^6.26.0",
5859
"css-loader": "^6.2.0",
@@ -67,12 +68,13 @@
6768
"rollup-plugin-uglify": "^6.0.0",
6869
"tinymce-4": "npm:tinymce@^4",
6970
"tinymce-5": "npm:tinymce@^5",
70-
"ts-loader": "^9.2.5",
71+
"ts-loader": "^9.2.6",
7172
"ts-node": "^10.2.1",
72-
"tsconfig-paths-webpack-plugin": "^3.5.1",
73+
"tsconfig-paths-webpack-plugin": "^3.5.2",
7374
"typescript": "^4.3.5",
74-
"vite": "^2.5.1",
75-
"vue": "^3.0.0",
75+
"vite": "^2.7.13",
76+
"vue": "^3.2.5",
77+
"vue-cli-plugin-storybook": "^2.1.0",
7678
"vue-loader": "^15.2.1",
7779
"vue-router": "^4.0.0-beta.13",
7880
"vue-template-compiler": "^2.6.14",

src/demo/Demo.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<router-link :class="{ active: $route.path==='/inline'}" to="/inline">Inline Editor</router-link>
77
<router-link :class="{ active: $route.path==='/controlled'}" to="/controlled">Controlled component</router-link>
88
<router-link :class="{ active: $route.path==='/keepalive'}" to="/keepalive">Keep-alive</router-link>
9+
<router-link :class="{ active: $route.path==='/refreshable'}" to="/refreshable">Rerender</router-link>
10+
<router-link :class="{ active: $route.path==='/tagged'}" to="/tagged">Tag Change</router-link>
911
</div>
1012
<router-view></router-view>
1113
</div>
@@ -15,5 +17,5 @@
1517
import { defineComponent } from "vue";
1618
export default defineComponent({
1719
name: 'Demo',
18-
})
20+
});
1921
</script>

src/demo/demo.ts

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Iframe from '/views/Iframe.vue';
77
import Inline from '/views/Inline.vue';
88
import Controlled from '/views/Controlled.vue';
99
import Keepalive from '/views/KeepAlive.vue';
10+
import Refreshable from '/views/Refreshable.vue';
11+
import Tagged from '/views/Tagged.vue';
1012

1113
const routes = [
1214
{
@@ -33,6 +35,16 @@ const routes = [
3335
path: '/keepalive',
3436
name: 'Keepalive',
3537
component: Keepalive
38+
},
39+
{
40+
path: '/refreshable',
41+
name: 'Refreshable',
42+
component: Refreshable
43+
},
44+
{
45+
path: '/tagged',
46+
name: 'Tagged',
47+
component: Tagged
3648
}
3749
];
3850

src/main/ts/components/Editor.ts

+25-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { ScriptLoader } from '../ScriptLoader';
1010
import { getTinymce } from '../TinyMCE';
1111
import { isTextarea, mergePlugins, uuid, isNullOrUndefined, initEditor } from '../Utils';
1212
import { editorProps } from './EditorPropTypes';
13-
import { h, defineComponent, onMounted, ref, Ref, toRefs, watch, onBeforeUnmount, onActivated, onDeactivated } from 'vue';
14-
import { Editor as TinyMCEEditor, EditorEvent } from 'tinymce';
13+
import { h, defineComponent, onMounted, ref, Ref, toRefs, nextTick, watch, onBeforeUnmount, onActivated, onDeactivated } from 'vue';
14+
import { Editor as TinyMCEEditor, EditorEvent, RawEditorSettings } from 'tinymce';
1515

1616
const renderInline = (ce: any, id: string, elementRef: Ref<Element | null>, tagName?: string) =>
1717
ce(tagName ? tagName : 'div', {
@@ -29,7 +29,8 @@ const renderIframe = (ce: any, id: string, elementRef: Ref<Element | null>) =>
2929
export const Editor = defineComponent({
3030
props: editorProps,
3131
setup: (props, ctx) => {
32-
const { disabled, modelValue } = toRefs(props);
32+
let conf = props.init ? {...props.init} : {};
33+
const { disabled, modelValue, tagName } = toRefs(props);
3334
const element: Ref<Element | null> = ref(null);
3435
let vueEditor: any = null;
3536
const elementId: string = props.id || uuid('tiny-vue');
@@ -46,17 +47,17 @@ export const Editor = defineComponent({
4647
const initWrapper = (): void => {
4748
const content = getContent(mounting);
4849
const finalInit = {
49-
...props.init,
50+
...conf,
5051
readonly: props.disabled,
5152
selector: `#${elementId}`,
52-
plugins: mergePlugins(props.init && props.init.plugins, props.plugins),
53-
toolbar: props.toolbar || (props.init && props.init.toolbar),
53+
plugins: mergePlugins(conf.plugins, props.plugins),
54+
toolbar: props.toolbar || (conf.toolbar),
5455
inline: inlineEditor,
5556
setup: (editor: TinyMCEEditor) => {
5657
vueEditor = editor;
5758
editor.on('init', (e: EditorEvent<any>) => initEditor(e, props, ctx, editor, modelValue, content));
58-
if (props.init && typeof props.init.setup === 'function') {
59-
props.init.setup(editor);
59+
if (typeof conf.setup === 'function') {
60+
conf.setup(editor);
6061
}
6162
}
6263
};
@@ -71,6 +72,13 @@ export const Editor = defineComponent({
7172
vueEditor.setMode(disable ? 'readonly' : 'design');
7273
}
7374
});
75+
watch(tagName, (tagName) => {
76+
if (!modelBind) {
77+
cache = vueEditor.getContent();
78+
}
79+
getTinymce()?.remove(vueEditor);
80+
nextTick(() => initWrapper());
81+
});
7482
onMounted(() => {
7583
if (getTinymce() !== null) {
7684
initWrapper();
@@ -105,6 +113,15 @@ export const Editor = defineComponent({
105113
getTinymce()?.remove(vueEditor);
106114
});
107115
}
116+
const rerender = (init: RawEditorSettings) => {
117+
cache = vueEditor.getContent();
118+
getTinymce()?.remove(vueEditor);
119+
conf = {...conf, ...init};
120+
nextTick(() => initWrapper());
121+
};
122+
ctx.expose({
123+
rerender
124+
});
108125
return () => inlineEditor ?
109126
renderInline(h, elementId, element, props.tagName) :
110127
renderIframe(h, elementId, element);

vite.config.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
const path = require('path');
1+
import vue from '@vitejs/plugin-vue';
2+
import path from 'path';
23

3-
module.exports = {
4+
export default {
45
root: 'src/demo',
5-
alias: {
6-
'/@/': path.resolve(__dirname, 'src'),
6+
plugins: [vue()],
7+
resolve: {
8+
alias: [
9+
{
10+
find: '/@',
11+
replacement: path.resolve(__dirname, './src')
12+
}
13+
]
714
},
8-
port: 3001
15+
server: {
16+
port: 3001
17+
}
918
};

0 commit comments

Comments
 (0)