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'
export interface ExtensionSpec<Options = {}, Commands = {}> {
@@ -123,10 +124,10 @@ export interface ExtensionSpec<Options = {}, Commands = {}> {
options: Options,
},
) => GlobalAttributes,
createCommands?(this: {
createCommands?: (this: {
options: Options,
// editor: Editor,
}): Commands,
editor: Editor,
}) => Commands,
}
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',
name: 'extension',
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 { ExtensionSpec } from './Extension'
import { ExtensionSpec, defaultExtension } from './Extension'
import { Attributes } from './types'
export interface MarkExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
@@ -66,10 +17,8 @@ export interface MarkExtensionSpec<Options = {}, Commands = {}> 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<Omit<MarkExtensionSpec, 'defaultOptions'> &
}>
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<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 { ExtensionSpec } from './Extension'
import { ExtensionSpec, defaultExtension } from './Extension'
import { Attributes } from './types'
export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
@@ -96,9 +25,7 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Extensio
},
props: {
node: Node,
attributes: {
[key: string]: any,
},
attributes: { [key: string]: any },
}
) => DOMOutputSpec,
createAttributes?: (
@@ -116,9 +43,9 @@ export type NodeExtension = Required<Omit<NodeExtensionSpec, 'defaultOptions'> &
}>
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: () => ({}),

View File

@@ -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 => {

View File

@@ -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]

View File

@@ -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',
}),
},