replace extensions

This commit is contained in:
Philipp Kühn
2020-09-08 23:44:45 +02:00
parent 678b6444d2
commit 26ecc20a50
16 changed files with 590 additions and 491 deletions

View File

@@ -1,54 +1,151 @@
import cloneDeep from 'clone-deep'
import { Plugin } from 'prosemirror-state'
import { Editor, Command } from './Editor'
import { Editor, CommandSpec } from './Editor'
export default abstract class Extension {
// export default abstract class Extension {
constructor(options = {}) {
this.options = {
...this.defaultOptions(),
...options,
// constructor(options = {}) {
// this.options = {
// ...this.defaultOptions(),
// ...options,
// }
// }
// editor!: Editor
// options: { [key: string]: any } = {}
// public abstract name: string
// public extensionType = 'extension'
// public created() {}
// public bindEditor(editor: Editor): void {
// this.editor = editor
// }
// defaultOptions(): { [key: string]: any } {
// return {}
// }
// update(): any {
// return () => {}
// }
// plugins(): Plugin[] {
// return []
// }
// inputRules(): any {
// return []
// }
// pasteRules(): any {
// return []
// }
// keys(): { [key: string]: Function } {
// return {}
// }
// commands(): { [key: string]: Command } {
// return {}
// }
// }
type AnyObject = {
[key: string]: any
}
type NoInfer<T> = [T][T extends any ? 0 : never]
export interface ExtensionCallback {
name: string
editor: Editor
options: any
}
export interface ExtensionExtends<Callback = ExtensionCallback> {
name: string
options: AnyObject
commands: (params: Callback) => CommandSpec
inputRules: (params: Callback) => any[]
pasteRules: (params: Callback) => any[]
keys: (params: Callback) => {
[key: string]: Function
}
plugins: (params: Callback) => Plugin[]
}
export default class Extension<Options, Extends extends ExtensionExtends = ExtensionExtends> {
type = 'extension'
configs: any = {}
usedOptions: Partial<Options> = {}
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
const item = {
stategy,
value,
}
if (this.configs[key]) {
this.configs[key].push(item)
} else {
this.configs[key] = [item]
}
}
public configure(options: Partial<Options>) {
this.usedOptions = { ...this.usedOptions, ...options }
return this
}
public name(value: Extends['name']) {
this.storeConfig('name', value, 'overwrite')
return this
}
public defaults(value: Options) {
this.storeConfig('defaults', value, 'overwrite')
return this
}
public commands(value: Extends['commands']) {
this.storeConfig('commands', value, 'overwrite')
return this
}
public keys(value: Extends['keys']) {
this.storeConfig('keys', value, 'overwrite')
return this
}
public inputRules(value: Extends['inputRules']) {
this.storeConfig('inputRules', value, 'overwrite')
return this
}
public pasteRules(value: Extends['pasteRules']) {
this.storeConfig('pasteRules', value, 'overwrite')
return this
}
public plugins(value: Extends['plugins']) {
this.storeConfig('plugins', value, 'overwrite')
return this
}
public extend<T extends Extract<keyof Extends, string>>(key: T, value: Extends[T]) {
this.storeConfig(key, value, 'extend')
return this
}
editor!: Editor
options: { [key: string]: any } = {}
public abstract name: string
public extensionType = 'extension'
public create() {
type ParentOptions = Options
public created() {}
public bindEditor(editor: Editor): void {
this.editor = editor
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
return cloneDeep(this, true).configure(options as Options)
}
}
defaultOptions(): { [key: string]: any } {
return {}
}
update(): any {
return () => {}
}
plugins(): Plugin[] {
return []
}
inputRules(): any {
return []
}
pasteRules(): any {
return []
}
keys(): { [key: string]: Function } {
return {}
}
commands(): { [key: string]: Command } {
return {}
}
}

View File

@@ -1,18 +1,37 @@
import Extension from './Extension'
import { MarkSpec } from 'prosemirror-model'
import { MarkSpec, MarkType } from 'prosemirror-model'
import Extension, { ExtensionCallback, ExtensionExtends } from './Extension'
export default abstract class Mark extends Extension {
// export default abstract class Mark extends Extension {
constructor(options = {}) {
super(options)
}
// constructor(options = {}) {
// super(options)
// }
public extensionType = 'mark'
// public extensionType = 'mark'
abstract schema(): MarkSpec
// abstract schema(): MarkSpec
get type() {
return this.editor.schema.marks[this.name]
}
// get type() {
// return this.editor.schema.marks[this.name]
// }
// }
export interface MarkCallback extends ExtensionCallback {
// TODO: fix optional
type?: MarkType
}
export interface MarkExtends<Callback = MarkCallback> extends ExtensionExtends<Callback> {
topMark: boolean
schema: (params: Callback) => MarkSpec
}
export default class Mark<Options = {}> extends Extension<Options, MarkExtends> {
type = 'node'
public schema(value: MarkExtends['schema']) {
this.storeConfig('schema', value, 'overwrite')
return this
}
}

View File

@@ -1,20 +1,44 @@
import Extension from './Extension'
import { NodeSpec } from 'prosemirror-model'
import { NodeSpec, NodeType } from 'prosemirror-model'
import Extension, { ExtensionCallback, ExtensionExtends } from './Extension'
export default abstract class Node extends Extension {
// export default abstract class Node extends Extension {
constructor(options = {}) {
super(options)
}
// constructor(options = {}) {
// super(options)
// }
public extensionType = 'node'
// public extensionType = 'node'
public topNode = false
// public topNode = false
abstract schema(): NodeSpec
// abstract schema(): NodeSpec
get type() {
return this.editor.schema.nodes[this.name]
}
// get type() {
// return this.editor.schema.nodes[this.name]
// }
// }
export interface NodeCallback extends ExtensionCallback {
// TODO: fix optional
type?: NodeType
}
export interface NodeExtends<Callback = NodeCallback> extends ExtensionExtends<Callback> {
topNode: boolean
schema: (params: Callback) => NodeSpec
}
export default class Node<Options = {}> extends Extension<Options, NodeExtends> {
type = 'node'
public topNode(value: NodeExtends['topNode'] = true) {
this.storeConfig('topNode', value, 'overwrite')
return this
}
public schema(value: NodeExtends['schema']) {
this.storeConfig('schema', value, 'overwrite')
return this
}
}

View File

@@ -1,7 +1,7 @@
import { NodeSpec, NodeType } from "prosemirror-model";
import deepmerge from 'deepmerge'
import collect from 'collect.js'
import { Editor, CommandSpec } from '@tiptap/core'
import { Editor, CommandSpec, ComponentRenderer } from '@tiptap/core'
import cloneDeep from 'clone-deep'
import { Plugin } from "prosemirror-state";
@@ -815,7 +815,7 @@ class ExtensionTest<Options, Extends extends ExtensionExtends = ExtensionExtends
}
}
public options(options: Partial<Options>) {
public configure(options: Partial<Options>) {
this.usedOptions = { ...this.usedOptions, ...options }
return this
}
@@ -864,7 +864,7 @@ class ExtensionTest<Options, Extends extends ExtensionExtends = ExtensionExtends
type ParentOptions = Options
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
return cloneDeep(this, true).options(options as Options)
return cloneDeep(this, true).configure(options as Options)
}
}
}
@@ -922,11 +922,83 @@ const Suggestion = new NodeTest<TestOptions>()
}))
.create()
const Blub = new ExtensionTest<TestOptions>()
.name('bla')
.create()
// TODO: Erweitern
// const CustomHeadlineAligned = new Headline()
// .name('custom_headline')
// .extend('defaults', {
// levels: [1, 2, 3],
// class: 'font-xs text-outline text-center',
// alignments: ['left', 'center', 'right'],
// })
console.log(Suggestion(), Suggestion().topNode().options({ trigger: 'jo' }))
// const CustomHeadlineTag = new Headline()
// .name('custom_headline')
// .configure({
// class: 'custom-headline',
// })
// .schema(() => ({
// toDOM: () => ['h1', 0],
// toVue: Component,
// }))
// .merge('schema', () => ({
// parseDOM: [
// {
// tag: 'x-custom-headline',
// }
// ],
// }))
// const Blub = new ExtensionTest<TestOptions>()
// .name('bla')
// .create()
console.log(Suggestion())
// export const Suggestion = new Suggestion()
// .name('suggestion')
// .defaults({
// trigger: '@',
// levels: [1, 2, 3],
// })
// .extend('schema', ({ editor, name, type}) => ({
// // levels: [1, 2, 3, 4, 5, 6],
// toDOM: () => ['strong', 0],
// }))
// .create()
// const Mention = Suggestion()
// .name('mention')
// .configure({
// trigger: '@'
// })
// .create()
// const Hashtag = Suggestion()
// .name('hashtag')
// .options({
// trigger: '#'
// })
// .create()
// new Editor({
// extensions: [
// // Mention({}),
// // Hashtag(),
// // Suggestion({ trigger: '#'}).name('hashtag'),
// // Suggestion.option({ trigger: '@'}).name('mention'),
// ]
// })
// interface MentionOptions {
// trigger: string