diff --git a/packages/core/src/Extension.ts b/packages/core/src/Extension.ts index 9ed640ba..5143f823 100644 --- a/packages/core/src/Extension.ts +++ b/packages/core/src/Extension.ts @@ -113,6 +113,7 @@ // } // } +import { Editor } from './Editor' import { GlobalAttributes } from './types' export interface ExtensionSpec { @@ -123,10 +124,10 @@ export interface ExtensionSpec { options: Options, }, ) => GlobalAttributes, - createCommands?(this: { + createCommands?: (this: { options: Options, - // editor: Editor, - }): Commands, + editor: Editor, + }) => Commands, } export type Extension = Required & { @@ -136,7 +137,7 @@ export type Extension = Required & { }, }> -const defaultExtension: Extension = { +export const defaultExtension: Extension = { type: 'extension', name: 'extension', options: {}, diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts index 64a47914..6df8b351 100644 --- a/packages/core/src/Mark.ts +++ b/packages/core/src/Mark.ts @@ -1,54 +1,5 @@ -// import { MarkSpec, MarkType } from 'prosemirror-model' -// import Extension, { ExtensionMethods } from './Extension' -// import { Editor } from './Editor' - -// export interface MarkProps { -// name: string -// editor: Editor -// options: Options -// type: MarkType -// } - -// export interface MarkMethods extends ExtensionMethods { -// topMark: boolean -// schema: (params: Omit) => MarkSpec -// } - -// export default class Mark< -// Options = {}, -// Props = MarkProps, -// Methods extends MarkMethods = MarkMethods, -// > extends Extension { -// type = 'mark' - -// public schema(value: Methods['schema']) { -// this.storeConfig('schema', value, 'overwrite') -// return this -// } -// } - -// import Extension from './Extension' - -// export default class Node extends Extension { - -// type = 'mark' - -// createAttributes() { -// return {} -// } - -// parseHTML() { -// return [] -// } - -// renderHTML() { -// return [] -// } - -// } - import { DOMOutputSpec, MarkSpec, Mark } from 'prosemirror-model' -import { ExtensionSpec } from './Extension' +import { ExtensionSpec, defaultExtension } from './Extension' import { Attributes } from './types' export interface MarkExtensionSpec extends ExtensionSpec { @@ -66,10 +17,8 @@ export interface MarkExtensionSpec extends Extensio options: Options, }, props: { - node: Mark, - attributes: { - [key: string]: any, - }, + mark: Mark, + attributes: { [key: string]: any }, } ) => DOMOutputSpec, createAttributes?: ( @@ -87,17 +36,16 @@ export type MarkExtension = Required & }> const defaultMark: MarkExtension = { + ...defaultExtension, type: 'mark', name: 'mark', - options: {}, inclusive: null, excludes: null, group: null, spanning: null, - createGlobalAttributes: () => [], - createCommands: () => ({}), parseHTML: () => null, renderHTML: () => null, + createAttributes: () => ({}), } export function createMark(config: MarkExtensionSpec) { diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index df8cd74c..3a3d3019 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -1,76 +1,5 @@ -// import { NodeSpec, NodeType } from 'prosemirror-model' -// import Extension, { ExtensionMethods } from './Extension' -// import { Editor } from './Editor' - -// export interface NodeProps { -// name: string -// editor: Editor -// options: Options -// type: NodeType -// } - -// export interface NodeMethods extends ExtensionMethods { -// topNode: boolean -// schema: (params: Omit) => NodeSpec -// } - -// export default class Node< -// Options = {}, -// Props = NodeProps, -// Methods extends NodeMethods = NodeMethods, -// > extends Extension { -// type = 'node' - -// public topNode(value: Methods['topNode'] = true) { -// this.storeConfig('topNode', value, 'overwrite') -// return this -// } - -// public schema(value: Methods['schema']) { -// this.storeConfig('schema', value, 'overwrite') -// return this -// } -// } - -// import { DOMOutputSpec, DOMOutputSpecArray } from 'prosemirror-model' -// import Extension from './Extension' - -// export interface INode { -// type: string -// topNode: boolean -// group: string -// content: string -// createAttributes(): any -// parseHTML(): any -// renderHTML(props: number): DOMOutputSpec -// } - -// export default class Node extends Extension implements INode { - -// type = 'node' - -// topNode = false - -// group = '' - -// content = '' - -// createAttributes() { -// return {} -// } - -// parseHTML() { -// return [] -// } - -// renderHTML() { -// return null -// } - -// } - import { DOMOutputSpec, NodeSpec, Node } from 'prosemirror-model' -import { ExtensionSpec } from './Extension' +import { ExtensionSpec, defaultExtension } from './Extension' import { Attributes } from './types' export interface NodeExtensionSpec extends ExtensionSpec { @@ -96,9 +25,7 @@ export interface NodeExtensionSpec extends Extensio }, props: { node: Node, - attributes: { - [key: string]: any, - }, + attributes: { [key: string]: any }, } ) => DOMOutputSpec, createAttributes?: ( @@ -116,9 +43,9 @@ export type NodeExtension = Required & }> const defaultNode: NodeExtension = { + ...defaultExtension, type: 'node', name: 'node', - options: {}, topNode: false, content: null, marks: null, @@ -130,8 +57,6 @@ const defaultNode: NodeExtension = { code: null, defining: null, isolating: null, - createGlobalAttributes: () => [], - createCommands: () => ({}), parseHTML: () => null, renderHTML: () => null, createAttributes: () => ({}), diff --git a/packages/core/src/utils/getRenderedAttributes.ts b/packages/core/src/utils/getRenderedAttributes.ts index bfb9a709..e702ee69 100644 --- a/packages/core/src/utils/getRenderedAttributes.ts +++ b/packages/core/src/utils/getRenderedAttributes.ts @@ -1,7 +1,7 @@ -import { Node } from 'prosemirror-model' +import { Node, Mark } from 'prosemirror-model' import { ExtensionAttribute } from '../types' -export default function getRenderedAttributes(node: Node, attributes: ExtensionAttribute[]) { +export default function getRenderedAttributes(node: Node | Mark, attributes: ExtensionAttribute[]) { return attributes .filter(item => item.attribute.rendered) .map(item => { diff --git a/packages/core/src/utils/getSchema.ts b/packages/core/src/utils/getSchema.ts index 0000471c..25aad7b3 100644 --- a/packages/core/src/utils/getSchema.ts +++ b/packages/core/src/utils/getSchema.ts @@ -48,16 +48,12 @@ export default function getSchema(extensions: Extensions): Schema { return [extension.name, schema] })) - // console.log({ nodes }) - const marks = Object.fromEntries(markExtensions.map(extension => { const context = { options: extension.options, } - // const attributes = { - // class: 'test', - // } + const attributes = allAttributes.filter(attribute => attribute.type === extension.name) const schema: MarkSpec = { inclusive: extension.inclusive, @@ -65,7 +61,15 @@ export default function getSchema(extensions: Extensions): Schema { group: extension.group, spanning: extension.spanning, parseDOM: extension.parseHTML.bind(context)(), - toDOM: node => extension.renderHTML.bind(context)({ node, attributes: {} }), + toDOM: mark => { + return extension.renderHTML.bind(context)({ + mark, + attributes: getRenderedAttributes(mark, attributes), + }) + }, + attrs: Object.fromEntries(attributes.map(attribute => { + return [attribute.name, { default: attribute?.attribute?.default }] + })), } return [extension.name, schema] diff --git a/packages/extension-paragraph/index.ts b/packages/extension-paragraph/index.ts index cb9ecf71..891a9685 100644 --- a/packages/extension-paragraph/index.ts +++ b/packages/extension-paragraph/index.ts @@ -73,6 +73,7 @@ export default createNode({ align: { default: 'right', renderHTML: attributes => ({ + class: 'global', style: `text-align: ${attributes.align}`, }), }, @@ -87,7 +88,7 @@ export default createNode({ default: '123', rendered: true, renderHTML: attributes => ({ - class: `foo-${attributes.id}`, + // class: `foo-${attributes.id}`, id: 'foo', }), },