feat: pass node & mark extensions as a contextual variable to addGlobalAttributes

This commit is contained in:
Nick the Sick 2024-06-03 17:49:07 +02:00 committed by Nick Perez
parent 4a01716408
commit 3d3263911f
5 changed files with 15 additions and 6 deletions

View File

@ -108,8 +108,9 @@ declare module '@tiptap/core' {
name: string
options: Options
storage: Storage
extensions: (Node | Mark)[]
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addGlobalAttributes']
}) => GlobalAttributes | {}
}) => GlobalAttributes
/**
* This function adds commands to the editor

View File

@ -111,8 +111,9 @@ declare module '@tiptap/core' {
name: string
options: Options
storage: Storage
extensions: (Node | Mark)[]
parent: ParentConfig<MarkConfig<Options, Storage>>['addGlobalAttributes']
}) => GlobalAttributes | {}
}) => GlobalAttributes
/**
* This function adds commands to the editor

View File

@ -7,6 +7,7 @@ import { Editor } from './Editor.js'
import { getExtensionField } from './helpers/getExtensionField.js'
import { NodeConfig } from './index.js'
import { InputRule } from './InputRule.js'
import { Mark } from './Mark.js'
import { PasteRule } from './PasteRule.js'
import {
AnyConfig,
@ -111,8 +112,9 @@ declare module '@tiptap/core' {
name: string
options: Options
storage: Storage
extensions: (Node | Mark)[]
parent: ParentConfig<NodeConfig<Options, Storage>>['addGlobalAttributes']
}) => GlobalAttributes | {}
}) => GlobalAttributes
/**
* This function adds commands to the editor

View File

@ -5,7 +5,6 @@ import {
Attributes,
ExtensionAttribute,
Extensions,
GlobalAttributes,
} from '../types.js'
import { getExtensionField } from './getExtensionField.js'
import { splitExtensions } from './splitExtensions.js'
@ -32,6 +31,7 @@ export function getAttributesFromExtensions(extensions: Extensions): ExtensionAt
name: extension.name,
options: extension.options,
storage: extension.storage,
extensions: nodeAndMarkExtensions,
}
const addGlobalAttributes = getExtensionField<AnyConfig['addGlobalAttributes']>(
@ -44,8 +44,7 @@ export function getAttributesFromExtensions(extensions: Extensions): ExtensionAt
return
}
// TODO: remove `as GlobalAttributes`
const globalAttributes = addGlobalAttributes() as GlobalAttributes
const globalAttributes = addGlobalAttributes()
globalAttributes.forEach(globalAttribute => {
globalAttribute.types.forEach(type => {

View File

@ -152,7 +152,13 @@ export type ExtensionAttribute = {
}
export type GlobalAttributes = {
/**
* The node & mark types this attribute should be applied to.
*/
types: string[]
/**
* The attributes to add to the node or mark types.
*/
attributes: {
[key: string]: Attribute
}