Skip to content

Commit

Permalink
Add blockSeparator option to clipboardTextSerializer core extensi…
Browse files Browse the repository at this point in the history
…on (#5019)

* Update clipboardTextSerializer.ts

* Update clipboardTextSerializer.ts

* feat(core): add core extension options to editor options

---------

Co-authored-by: Dominik Biedebach <[email protected]>
  • Loading branch information
hivokas and bdbch authored Apr 8, 2024
1 parent 677642e commit b710783
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
19 changes: 16 additions & 3 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { EditorView } from '@tiptap/pm/view'
import { CommandManager } from './CommandManager.js'
import { EventEmitter } from './EventEmitter.js'
import { ExtensionManager } from './ExtensionManager.js'
import * as extensions from './extensions/index.js'
import {
ClipboardTextSerializer, Commands, Editable, FocusEvents, Keymap, Tabindex,
} from './extensions/index.js'
import { createDocument } from './helpers/createDocument.js'
import { getAttributes } from './helpers/getAttributes.js'
import { getHTMLFromFragment } from './helpers/getHTMLFromFragment.js'
Expand All @@ -32,7 +34,7 @@ import {
import { createStyleTag } from './utilities/createStyleTag.js'
import { isFunction } from './utilities/isFunction.js'

export { extensions }
export * as extensions from './extensions/index.js'

export interface HTMLElement {
editor?: Editor
Expand Down Expand Up @@ -63,6 +65,7 @@ export class Editor extends EventEmitter<EditorEvents> {
editable: true,
editorProps: {},
parseOptions: {},
coreExtensionOptions: {},
enableInputRules: true,
enablePasteRules: true,
enableCoreExtensions: true,
Expand Down Expand Up @@ -235,7 +238,17 @@ export class Editor extends EventEmitter<EditorEvents> {
* Creates an extension manager.
*/
private createExtensionManager(): void {
const coreExtensions = this.options.enableCoreExtensions ? Object.values(extensions) : []

const coreExtensions = this.options.enableCoreExtensions ? [
Editable,
ClipboardTextSerializer.configure({
blockSeparator: this.options.coreExtensionOptions?.clipboardTextSerializer?.blockSeparator,
}),
Commands,
FocusEvents,
Keymap,
Tabindex,
] : []
const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
return ['extension', 'node', 'mark'].includes(extension?.type)
})
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/extensions/clipboardTextSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import { Extension } from '../Extension.js'
import { getTextBetween } from '../helpers/getTextBetween.js'
import { getTextSerializersFromSchema } from '../helpers/getTextSerializersFromSchema.js'

export const ClipboardTextSerializer = Extension.create({
export type ClipboardTextSerializerOptions = {
blockSeparator?: string,
}

export const ClipboardTextSerializer = Extension.create<ClipboardTextSerializerOptions>({
name: 'clipboardTextSerializer',

addOptions() {
return {
blockSeparator: undefined,
}
},

addProseMirrorPlugins() {
return [
new Plugin({
Expand All @@ -23,6 +33,9 @@ export const ClipboardTextSerializer = Extension.create({
const range = { from, to }

return getTextBetween(doc, range, {
...(this.options.blockSeparator !== undefined
? { blockSeparator: this.options.blockSeparator }
: {}),
textSerializers,
})
},
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export interface EditorOptions {
editable: boolean
editorProps: EditorProps
parseOptions: ParseOptions
coreExtensionOptions?: {
clipboardTextSerializer?: {
blockSeparator?: string
}
}
enableInputRules: EnableRules
enablePasteRules: EnableRules
enableCoreExtensions: boolean
Expand Down

0 comments on commit b710783

Please sign in to comment.