From e6f67caef37d3fb73adadfb64060541a0b791924 Mon Sep 17 00:00:00 2001 From: Robert van Hoesel Date: Mon, 9 Aug 2021 17:19:50 +0200 Subject: [PATCH] fix: fix updating editorProps via setOptions (#1540), fix #1518 --- packages/core/src/Editor.ts | 9 +++ .../integration/core/editorProps.spec.ts | 55 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/cypress/integration/core/editorProps.spec.ts diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index fc15f69d..71eb2341 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -133,6 +133,12 @@ export class Editor extends EventEmitter { */ public setOptions(options: Partial = {}): void { this.options = { ...this.options, ...options } + + // Update editorProps directly on the view and store reference to configured props + if (this.view) { + if (options.editorProps) this.view.setProps(options.editorProps) + this.options.editorProps = this.view.props + } } /** @@ -251,6 +257,9 @@ export class Editor extends EventEmitter { // So we’ll have access to it for tests. const dom = this.view.dom as HTMLElement dom.editor = this + + // Reference the resulting view props in our options + this.options.editorProps = this.view.props } /** diff --git a/tests/cypress/integration/core/editorProps.spec.ts b/tests/cypress/integration/core/editorProps.spec.ts new file mode 100644 index 00000000..0053d886 --- /dev/null +++ b/tests/cypress/integration/core/editorProps.spec.ts @@ -0,0 +1,55 @@ +/// + +import { Editor } from '@tiptap/core' +import Document from '@tiptap/extension-document' +import Paragraph from '@tiptap/extension-paragraph' +import Text from '@tiptap/extension-text' + +describe('editorProps', () => { + it('editorProps can be set while constructing Editor', () => { + function transformPastedHTML(html: string) { + return html + } + + const editor = new Editor({ + extensions: [Document, Paragraph, Text], + editorProps: { transformPastedHTML }, + }) + + expect(transformPastedHTML) + .to.eq(editor.options.editorProps.transformPastedHTML) + .and.to.eq(editor.options.editorProps.transformPastedHTML) + }) + + it('editorProps can be set through setOptions', () => { + function transformPastedHTML(html: string) { + return html + } + + const editor = new Editor({ + extensions: [Document, Paragraph, Text], + }) + + editor.setOptions({ editorProps: { transformPastedHTML } }) + + expect(transformPastedHTML) + .to.eq(editor.options.editorProps.transformPastedHTML) + .and.to.eq(editor.options.editorProps.transformPastedHTML) + }) + + it('editorProps can be set directly through options', () => { + function transformPastedHTML(html: string) { + return html + } + + const editor = new Editor({ + extensions: [Document, Paragraph, Text], + }) + + editor.options.editorProps.transformPastedHTML = transformPastedHTML + + expect(transformPastedHTML) + .to.eq(editor.options.editorProps.transformPastedHTML) + .and.to.eq(editor.options.editorProps.transformPastedHTML) + }) +})