refactoring

This commit is contained in:
Philipp Kühn
2020-10-22 09:14:24 +02:00
parent 5dcbdebbb7
commit faa65b5450
6 changed files with 27 additions and 148 deletions

View File

@@ -113,6 +113,7 @@
// } // }
// } // }
import { Editor } from './Editor'
import { GlobalAttributes } from './types' import { GlobalAttributes } from './types'
export interface ExtensionSpec<Options = {}, Commands = {}> { export interface ExtensionSpec<Options = {}, Commands = {}> {
@@ -123,10 +124,10 @@ export interface ExtensionSpec<Options = {}, Commands = {}> {
options: Options, options: Options,
}, },
) => GlobalAttributes, ) => GlobalAttributes,
createCommands?(this: { createCommands?: (this: {
options: Options, options: Options,
// editor: Editor, editor: Editor,
}): Commands, }) => Commands,
} }
export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & { export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & {
@@ -136,7 +137,7 @@ export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & {
}, },
}> }>
const defaultExtension: Extension = { export const defaultExtension: Extension = {
type: 'extension', type: 'extension',
name: 'extension', name: 'extension',
options: {}, options: {},

View File

@@ -1,54 +1,5 @@
// import { MarkSpec, MarkType } from 'prosemirror-model'
// import Extension, { ExtensionMethods } from './Extension'
// import { Editor } from './Editor'
// export interface MarkProps<Options> {
// name: string
// editor: Editor
// options: Options
// type: MarkType
// }
// export interface MarkMethods<Props, Options> extends ExtensionMethods<Props, Options> {
// topMark: boolean
// schema: (params: Omit<Props, 'type' | 'editor'>) => MarkSpec
// }
// export default class Mark<
// Options = {},
// Props = MarkProps<Options>,
// Methods extends MarkMethods<Props, Options> = MarkMethods<Props, Options>,
// > extends Extension<Options, Props, Methods> {
// type = 'mark'
// public schema(value: Methods['schema']) {
// this.storeConfig('schema', value, 'overwrite')
// return this
// }
// }
// import Extension from './Extension'
// export default class Node<Options = {}> extends Extension<Options> {
// type = 'mark'
// createAttributes() {
// return {}
// }
// parseHTML() {
// return []
// }
// renderHTML() {
// return []
// }
// }
import { DOMOutputSpec, MarkSpec, Mark } from 'prosemirror-model' import { DOMOutputSpec, MarkSpec, Mark } from 'prosemirror-model'
import { ExtensionSpec } from './Extension' import { ExtensionSpec, defaultExtension } from './Extension'
import { Attributes } from './types' import { Attributes } from './types'
export interface MarkExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> { export interface MarkExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
@@ -66,10 +17,8 @@ export interface MarkExtensionSpec<Options = {}, Commands = {}> extends Extensio
options: Options, options: Options,
}, },
props: { props: {
node: Mark, mark: Mark,
attributes: { attributes: { [key: string]: any },
[key: string]: any,
},
} }
) => DOMOutputSpec, ) => DOMOutputSpec,
createAttributes?: ( createAttributes?: (
@@ -87,17 +36,16 @@ export type MarkExtension = Required<Omit<MarkExtensionSpec, 'defaultOptions'> &
}> }>
const defaultMark: MarkExtension = { const defaultMark: MarkExtension = {
...defaultExtension,
type: 'mark', type: 'mark',
name: 'mark', name: 'mark',
options: {},
inclusive: null, inclusive: null,
excludes: null, excludes: null,
group: null, group: null,
spanning: null, spanning: null,
createGlobalAttributes: () => [],
createCommands: () => ({}),
parseHTML: () => null, parseHTML: () => null,
renderHTML: () => null, renderHTML: () => null,
createAttributes: () => ({}),
} }
export function createMark<Options extends {}, Commands extends {}>(config: MarkExtensionSpec<Options, Commands>) { export function createMark<Options extends {}, Commands extends {}>(config: MarkExtensionSpec<Options, Commands>) {

View File

@@ -1,76 +1,5 @@
// import { NodeSpec, NodeType } from 'prosemirror-model'
// import Extension, { ExtensionMethods } from './Extension'
// import { Editor } from './Editor'
// export interface NodeProps<Options> {
// name: string
// editor: Editor
// options: Options
// type: NodeType
// }
// export interface NodeMethods<Props, Options> extends ExtensionMethods<Props, Options> {
// topNode: boolean
// schema: (params: Omit<Props, 'type' | 'editor'>) => NodeSpec
// }
// export default class Node<
// Options = {},
// Props = NodeProps<Options>,
// Methods extends NodeMethods<Props, Options> = NodeMethods<Props, Options>,
// > extends Extension<Options, Props, Methods> {
// 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<Options = {}> extends Extension<Options> implements INode {
// type = 'node'
// topNode = false
// group = ''
// content = ''
// createAttributes() {
// return {}
// }
// parseHTML() {
// return []
// }
// renderHTML() {
// return null
// }
// }
import { DOMOutputSpec, NodeSpec, Node } from 'prosemirror-model' import { DOMOutputSpec, NodeSpec, Node } from 'prosemirror-model'
import { ExtensionSpec } from './Extension' import { ExtensionSpec, defaultExtension } from './Extension'
import { Attributes } from './types' import { Attributes } from './types'
export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> { export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
@@ -96,9 +25,7 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Extensio
}, },
props: { props: {
node: Node, node: Node,
attributes: { attributes: { [key: string]: any },
[key: string]: any,
},
} }
) => DOMOutputSpec, ) => DOMOutputSpec,
createAttributes?: ( createAttributes?: (
@@ -116,9 +43,9 @@ export type NodeExtension = Required<Omit<NodeExtensionSpec, 'defaultOptions'> &
}> }>
const defaultNode: NodeExtension = { const defaultNode: NodeExtension = {
...defaultExtension,
type: 'node', type: 'node',
name: 'node', name: 'node',
options: {},
topNode: false, topNode: false,
content: null, content: null,
marks: null, marks: null,
@@ -130,8 +57,6 @@ const defaultNode: NodeExtension = {
code: null, code: null,
defining: null, defining: null,
isolating: null, isolating: null,
createGlobalAttributes: () => [],
createCommands: () => ({}),
parseHTML: () => null, parseHTML: () => null,
renderHTML: () => null, renderHTML: () => null,
createAttributes: () => ({}), createAttributes: () => ({}),

View File

@@ -1,7 +1,7 @@
import { Node } from 'prosemirror-model' import { Node, Mark } from 'prosemirror-model'
import { ExtensionAttribute } from '../types' import { ExtensionAttribute } from '../types'
export default function getRenderedAttributes(node: Node, attributes: ExtensionAttribute[]) { export default function getRenderedAttributes(node: Node | Mark, attributes: ExtensionAttribute[]) {
return attributes return attributes
.filter(item => item.attribute.rendered) .filter(item => item.attribute.rendered)
.map(item => { .map(item => {

View File

@@ -48,16 +48,12 @@ export default function getSchema(extensions: Extensions): Schema {
return [extension.name, schema] return [extension.name, schema]
})) }))
// console.log({ nodes })
const marks = Object.fromEntries(markExtensions.map(extension => { const marks = Object.fromEntries(markExtensions.map(extension => {
const context = { const context = {
options: extension.options, options: extension.options,
} }
// const attributes = { const attributes = allAttributes.filter(attribute => attribute.type === extension.name)
// class: 'test',
// }
const schema: MarkSpec = { const schema: MarkSpec = {
inclusive: extension.inclusive, inclusive: extension.inclusive,
@@ -65,7 +61,15 @@ export default function getSchema(extensions: Extensions): Schema {
group: extension.group, group: extension.group,
spanning: extension.spanning, spanning: extension.spanning,
parseDOM: extension.parseHTML.bind(context)(), 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] return [extension.name, schema]

View File

@@ -73,6 +73,7 @@ export default createNode({
align: { align: {
default: 'right', default: 'right',
renderHTML: attributes => ({ renderHTML: attributes => ({
class: 'global',
style: `text-align: ${attributes.align}`, style: `text-align: ${attributes.align}`,
}), }),
}, },
@@ -87,7 +88,7 @@ export default createNode({
default: '123', default: '123',
rendered: true, rendered: true,
renderHTML: attributes => ({ renderHTML: attributes => ({
class: `foo-${attributes.id}`, // class: `foo-${attributes.id}`,
id: 'foo', id: 'foo',
}), }),
}, },