Skip to content

Commit

Permalink
feat: Allow array of extensions for enableInputRules and `enablePas…
Browse files Browse the repository at this point in the history
…teRules` options (#2119)
  • Loading branch information
aguingand authored Nov 18, 2021
1 parent 7e1ca4c commit 3158c37
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import getNodeType from './helpers/getNodeType'
import splitExtensions from './helpers/splitExtensions'
import getAttributesFromExtensions from './helpers/getAttributesFromExtensions'
import getRenderedAttributes from './helpers/getRenderedAttributes'
import isExtensionRulesEnabled from './helpers/isExtensionRulesEnabled'
import callOrReturn from './utilities/callOrReturn'
import findDuplicates from './utilities/findDuplicates'
import { NodeConfig } from '.'
Expand Down Expand Up @@ -270,7 +271,7 @@ export default class ExtensionManager {
context,
)

if (editor.options.enableInputRules && addInputRules) {
if (isExtensionRulesEnabled(extension, editor.options.enableInputRules) && addInputRules) {
inputRules.push(...addInputRules())
}

Expand All @@ -280,7 +281,7 @@ export default class ExtensionManager {
context,
)

if (editor.options.enablePasteRules && addPasteRules) {
if (isExtensionRulesEnabled(extension, editor.options.enablePasteRules) && addPasteRules) {
pasteRules.push(...addPasteRules())
}

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/helpers/isExtensionRulesEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AnyExtension, EnableRules } from '@tiptap/core'

export default function isExtensionRulesEnabled(extension: AnyExtension, enabled: EnableRules): boolean {
if (Array.isArray(enabled)) {
return enabled.some(enabledExtension => {
const name = typeof enabledExtension === 'string'
? enabledExtension
: enabledExtension.name

return name === extension.name
})
}

return enabled
}
6 changes: 4 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export interface EditorEvents {
destroy: void,
}

export type EnableRules = (AnyExtension | string)[] | boolean

export interface EditorOptions {
element: Element,
content: Content,
Expand All @@ -72,8 +74,8 @@ export interface EditorOptions {
editable: boolean,
editorProps: EditorProps,
parseOptions: ParseOptions,
enableInputRules: boolean,
enablePasteRules: boolean,
enableInputRules: EnableRules,
enablePasteRules: EnableRules,
enableCoreExtensions: boolean,
onBeforeCreate: (props: EditorEvents['beforeCreate']) => void,
onCreate: (props: EditorEvents['create']) => void,
Expand Down

0 comments on commit 3158c37

Please sign in to comment.