Skip to content

Commit

Permalink
feat(core): add editor to this context in schema functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbch committed Mar 29, 2023
1 parent 9b855c0 commit 1470bed
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ExtensionManager {
constructor(extensions: Extensions, editor: Editor) {
this.editor = editor
this.extensions = ExtensionManager.resolve(extensions)
this.schema = getSchemaByResolvedExtensions(this.extensions)
this.schema = getSchemaByResolvedExtensions(this.extensions, editor)

this.extensions.forEach(extension => {
// store extension storage in editor
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/Mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['inclusive']
editor?: Editor
}) => MarkSpec['inclusive'])

/**
Expand All @@ -323,6 +324,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['excludes']
editor?: Editor
}) => MarkSpec['excludes'])

/**
Expand All @@ -340,6 +342,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['group']
editor?: Editor
}) => MarkSpec['group'])

/**
Expand All @@ -352,6 +355,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['spanning']
editor?: Editor
}) => MarkSpec['spanning'])

/**
Expand All @@ -364,6 +368,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['code']
editor?: Editor
}) => boolean)

/**
Expand All @@ -374,6 +379,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['parseHTML']
editor?: Editor
}) => MarkSpec['parseDOM']

/**
Expand All @@ -386,6 +392,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['renderHTML']
editor?: Editor
},
props: {
mark: ProseMirrorMark
Expand All @@ -402,6 +409,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<MarkConfig<Options, Storage>>['addAttributes']
editor?: Editor
}) => Attributes | {}
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['extendMarkSchema']
editor?: Editor
},
extension: Node,
) => Record<string, any>)
Expand Down Expand Up @@ -325,6 +326,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['content']
editor?: Editor
}) => NodeSpec['content'])

/**
Expand All @@ -337,6 +339,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['marks']
editor?: Editor
}) => NodeSpec['marks'])

/**
Expand All @@ -349,6 +352,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['group']
editor?: Editor
}) => NodeSpec['group'])

/**
Expand All @@ -361,6 +365,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['inline']
editor?: Editor
}) => NodeSpec['inline'])

/**
Expand All @@ -373,6 +378,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['atom']
editor?: Editor
}) => NodeSpec['atom'])

/**
Expand All @@ -385,6 +391,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['selectable']
editor?: Editor
}) => NodeSpec['selectable'])

/**
Expand All @@ -397,6 +404,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['draggable']
editor?: Editor
}) => NodeSpec['draggable'])

/**
Expand All @@ -409,6 +417,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['code']
editor?: Editor
}) => NodeSpec['code'])

/**
Expand All @@ -421,6 +430,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['whitespace']
editor?: Editor
}) => NodeSpec['whitespace'])

/**
Expand All @@ -433,6 +443,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['defining']
editor?: Editor
}) => NodeSpec['defining'])

/**
Expand All @@ -445,6 +456,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['isolating']
editor?: Editor
}) => NodeSpec['isolating'])

/**
Expand All @@ -455,6 +467,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['parseHTML']
editor?: Editor
}) => NodeSpec['parseDOM']

/**
Expand All @@ -467,6 +480,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['renderHTML']
editor?: Editor
},
props: {
node: ProseMirrorNode
Expand All @@ -485,6 +499,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['renderText']
editor?: Editor
},
props: {
node: ProseMirrorNode
Expand All @@ -503,6 +518,7 @@ declare module '@tiptap/core' {
options: Options
storage: Storage
parent: ParentConfig<NodeConfig<Options, Storage>>['addAttributes']
editor?: Editor
}) => Attributes | {}
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/helpers/getSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Schema } from '@tiptap/pm/model'

import { Editor } from '../Editor'
import { ExtensionManager } from '../ExtensionManager'
import { Extensions } from '../types'
import { getSchemaByResolvedExtensions } from './getSchemaByResolvedExtensions'

export function getSchema(extensions: Extensions): Schema {
export function getSchema(extensions: Extensions, editor?: Editor): Schema {
const resolvedExtensions = ExtensionManager.resolve(extensions)

return getSchemaByResolvedExtensions(resolvedExtensions)
return getSchemaByResolvedExtensions(resolvedExtensions, editor)
}
9 changes: 6 additions & 3 deletions packages/core/src/helpers/getSchemaByResolvedExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MarkSpec, NodeSpec, Schema } from '@tiptap/pm/model'

import { MarkConfig, NodeConfig } from '..'
import { Editor, MarkConfig, NodeConfig } from '..'
import { AnyConfig, Extensions } from '../types'
import { callOrReturn } from '../utilities/callOrReturn'
import { isEmptyObject } from '../utilities/isEmptyObject'
Expand All @@ -12,8 +12,9 @@ import { splitExtensions } from './splitExtensions'

function cleanUpSchemaItem<T>(data: T) {
return Object.fromEntries(
// @ts-ignore
Object.entries(data).filter(([key, value]) => {
if (key === 'attrs' && isEmptyObject(value)) {
if (key === 'attrs' && isEmptyObject(value as {} | undefined)) {
return false
}

Expand All @@ -22,7 +23,7 @@ function cleanUpSchemaItem<T>(data: T) {
) as T
}

export function getSchemaByResolvedExtensions(extensions: Extensions): Schema {
export function getSchemaByResolvedExtensions(extensions: Extensions, editor?: Editor): Schema {
const allAttributes = getAttributesFromExtensions(extensions)
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const topNode = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))?.name
Expand All @@ -36,6 +37,7 @@ export function getSchemaByResolvedExtensions(extensions: Extensions): Schema {
name: extension.name,
options: extension.options,
storage: extension.storage,
editor,
}

const extraNodeFields = extensions.reduce((fields, e) => {
Expand Down Expand Up @@ -124,6 +126,7 @@ export function getSchemaByResolvedExtensions(extensions: Extensions): Schema {
name: extension.name,
options: extension.options,
storage: extension.storage,
editor,
}

const extraMarkFields = extensions.reduce((fields, e) => {
Expand Down

0 comments on commit 1470bed

Please sign in to comment.