Skip to content

Commit

Permalink
fix: 修复 tinymce 被移动后不可用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop authored and CheshireJCat committed Nov 29, 2024
1 parent 9eca095 commit ee37347
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions packages/amis-ui/src/components/Tinymce.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
// Import TinyMCE
// @ts-ignore
import tinymce from 'tinymce/tinymce';
import tinymce, {Editor} from 'tinymce/tinymce';

// A theme is also required
import 'tinymce/icons/default/index';
Expand Down Expand Up @@ -63,7 +63,7 @@ export default class TinymceEditor extends React.Component<TinymceEditorProps> {
outputFormat: 'html'
};
config?: any;
editor?: any;
editor?: Editor;
unmounted = false;
editorInitialized?: boolean = false;
currentContent?: string;
Expand All @@ -85,14 +85,25 @@ export default class TinymceEditor extends React.Component<TinymceEditorProps> {
this.editor?.setContent((this.currentContent = props.model || ''));
}

if (!isEqual(this.props.config, prevProps.config)) {
if (this.editor && !isEqual(this.props.config, prevProps.config)) {
this.editor.contentWindow.removeEventListener(
'unload',
this.handleIframeUnload
);
tinymce.remove(this.editor);
this.initTiny();
}
}

componentWillUnmount() {
tinymce.remove(this.editor);
if (this.editor) {
this.editor.contentWindow.removeEventListener(
'unload',
this.handleIframeUnload
);
tinymce.remove(this.editor);
}

this.unmounted = true;
}

Expand Down Expand Up @@ -199,7 +210,19 @@ export default class TinymceEditor extends React.Component<TinymceEditorProps> {
this.unmounted || tinymce.init(this.config);
}

initEditor(e: any, editor: any) {
@autobind
handleIframeUnload() {
this.editor!.contentWindow.removeEventListener(
'unload',
this.handleIframeUnload
);
requestAnimationFrame(() => {
tinymce.remove(this.editor!);
this.initTiny();
});
}

initEditor(e: any, editor: Editor) {
const {model, onModelChange, outputFormat, onFocus, onBlur} = this.props;

const value = model || '';
Expand All @@ -218,6 +241,10 @@ export default class TinymceEditor extends React.Component<TinymceEditorProps> {

onFocus && editor.on('focus', onFocus);
onBlur && editor.on('blur', onBlur);

// iframe 移动后,就不可用了,那只能重新初始化
// https://poeticcode.wordpress.com/2010/06/08/iframe-reloads-when-moved-around-the-dom-tree/
editor.contentWindow.addEventListener('unload', this.handleIframeUnload);
}

render() {
Expand Down

0 comments on commit ee37347

Please sign in to comment.