diff --git a/packages/plugins/clipboard-plugin/src/index.spec.ts b/packages/plugins/clipboard-plugin/src/index.spec.ts index f4bacd38..a2880235 100644 --- a/packages/plugins/clipboard-plugin/src/index.spec.ts +++ b/packages/plugins/clipboard-plugin/src/index.spec.ts @@ -206,7 +206,6 @@ describe('ClipboardPlugin', () => { new ClipboardPlugin(pluginParamsMock); const { setData } = dispatchCopyEvent(); - expect(setData).toHaveBeenCalledWith('text/plain', 'hello\nworld'); }); @@ -229,6 +228,28 @@ describe('ClipboardPlugin', () => { ); }); + it('should use core version from api when available', () => { + const api = pluginParamsMock.api as unknown as { version: string }; + + api.version = '3.2.1'; + + new ClipboardPlugin(pluginParamsMock); + + const { setData } = dispatchCopyEvent(); + + expect(setData).toHaveBeenCalledWith( + 'application/x-editor-js', + JSON.stringify({ + blocks: [ + { id: 'b1', + type: 'paragraph' }, + { id: 'b2', + type: 'header' }, + ], + meta: { version: '3.2.1' }, + }) + ); + }); it('should not prevent native event when clipboardData is unavailable', () => { new ClipboardPlugin(pluginParamsMock); diff --git a/packages/plugins/clipboard-plugin/src/index.ts b/packages/plugins/clipboard-plugin/src/index.ts index ac344c54..f16fad6b 100644 --- a/packages/plugins/clipboard-plugin/src/index.ts +++ b/packages/plugins/clipboard-plugin/src/index.ts @@ -27,6 +27,16 @@ interface Meta { version: string; } +/** + * Source that can provide EditorJS core version. + */ +interface CoreVersionSource { + /** + * EditorJS core version. + */ + version?: string; +} + /** * Custom MIME type used to store EditorJS clipboard data alongside plain text and HTML */ @@ -40,8 +50,11 @@ const EDITOR_JS_CLIPBOARD_MIME_TYPE = 'application/x-editor-js'; export class ClipboardPlugin implements EditorjsPlugin { public static readonly type = PluginType.Plugin; + static readonly #fallbackCoreVersion = '3.0.0'; + readonly #api: EditorAPI; readonly #eventBus: EventBus; + readonly #coreVersion: string; #copyEventListener: ((e: CopyUIEvent) => void) | undefined; /** @@ -55,6 +68,7 @@ export class ClipboardPlugin implements EditorjsPlugin { this.#api = api; this.#eventBus = eventBus; + this.#coreVersion = this.#getCoreVersion(params); this.#copyEventListener = (e: CopyUIEvent) => { const { nativeEvent } = e.detail; @@ -146,9 +160,29 @@ export class ClipboardPlugin implements EditorjsPlugin { return { blocks, meta: { - // @todo get version info from Core - version: '3.0.0', + version: this.#coreVersion, }, }; } + + /** + * Resolves EditorJS core version from runtime API/config when available. + * @param params - plugin initialization params + * @returns resolved core version + */ + #getCoreVersion(params: EditorjsPluginParams): string { + const apiVersion = (params.api as CoreVersionSource).version; + + if (apiVersion !== undefined && apiVersion.trim() !== '') { + return apiVersion; + } + + const configVersion = (params.config as CoreVersionSource).version; + + if (configVersion !== undefined && configVersion.trim() !== '') { + return configVersion; + } + + return ClipboardPlugin.#fallbackCoreVersion; + } }