Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 tinymce 被移动后不可用问题 #11286

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading