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

Add blockSeparator option to clipboardTextSerializer core extension #5019

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
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
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 ? [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only have less than 10 core extensions I'd think it's okay to just load them manually here – that way we can pass configurations down to them.

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