add some tests

This commit is contained in:
Philipp Kühn
2019-12-08 00:16:44 +01:00
parent d8fe4ee3f8
commit 90f86bafbb
12 changed files with 524 additions and 10 deletions

View File

@@ -1,3 +1,3 @@
export default function() {
return 1
}
import { Editor } from './src/Editor'
export default Editor

View File

@@ -4,11 +4,19 @@
"source": "index.ts",
"main": "dist/tiptap-core.js",
"umd:main": "dist/tiptap-core.umd.js",
"module": "dist/tiptap-core.mjs",
"module": "dist/tiptap-core.mjs",
"unpkg": "dist/tiptap-core.js",
"jsdelivr": "dist/tiptap-core.js",
"files": [
"src",
"dist"
]
],
"dependencies": {
"prosemirror-example-setup": "^1.1.2",
"prosemirror-model": "^1.8.2",
"prosemirror-schema-basic": "^1.1.2",
"prosemirror-schema-list": "^1.1.2",
"prosemirror-state": "^1.3.2",
"prosemirror-view": "^1.13.4"
}
}

View File

@@ -0,0 +1,132 @@
import {EditorState, Plugin} from "prosemirror-state"
import {EditorView} from "prosemirror-view"
import {Schema, DOMParser} from "prosemirror-model"
// @ts-ignore
import {schema} from "prosemirror-schema-basic"
// @ts-ignore
import {addListNodes} from "prosemirror-schema-list"
// @ts-ignore
import {exampleSetup} from "prosemirror-example-setup"
import insertText from './commands/insertText'
import focus from './commands/focus'
interface EditorOptions {
element: Node
content: string
}
export class Editor {
private lastCommand = Promise.resolve()
private schema: Schema = new Schema({
nodes: addListNodes(schema.spec.nodes, "paragraph block*", "block"),
marks: schema.spec.marks
})
selection = { from: 0, to: 0 }
view: EditorView
options: EditorOptions
constructor(options: EditorOptions) {
this.options = options
this.view = this.createView()
this.registerCommand('focus', focus)
this.registerCommand('insertText', insertText)
}
get state() {
return this.view.state
}
private createState() {
return EditorState.create({
doc: this.createDocument(this.options.content),
plugins: [
...exampleSetup({schema: this.schema}),
],
})
}
private createView() {
return new EditorView(this.options.element, {
state: this.createState(),
dispatchTransaction: this.dispatchTransaction.bind(this),
})
}
private chainCommand = (method: Function) => (...args: any) => {
this.lastCommand = this.lastCommand
.then(() => method.apply(this, args))
.catch(console.error)
return this
}
registerCommand(name: string, method: Function): Editor {
// @ts-ignore
this[name] = this.chainCommand((...args: any) => {
return new Promise(resolve => {
return method(resolve as Function, this as Editor, ...args as any)
})
})
return this
}
command(name: string, ...args: any) {
// @ts-ignore
return this[name](...args)
}
private createDocument(content: any): any {
// if (content === null) {
// return this.schema.nodeFromJSON(this.options.emptyDocument)
// }
// if (typeof content === 'object') {
// try {
// return this.schema.nodeFromJSON(content)
// } catch (error) {
// console.warn('[tiptap warn]: Invalid content.', 'Passed value:', content, 'Error:', error)
// return this.schema.nodeFromJSON(this.options.emptyDocument)
// }
// }
if (typeof content === 'string') {
const element = document.createElement('div')
element.innerHTML = content.trim()
return DOMParser.fromSchema(this.schema).parse(element)
}
return false
}
private dispatchTransaction(transaction: any): void {
const newState = this.state.apply(transaction)
this.view.updateState(newState)
this.selection = {
from: this.state.selection.from,
to: this.state.selection.to,
}
// this.setActiveNodesAndMarks()
// this.emit('transaction', {
// getHTML: this.getHTML.bind(this),
// getJSON: this.getJSON.bind(this),
// state: this.state,
// transaction,
// })
if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
return
}
// this.emitUpdate(transaction)
}
}

View File

@@ -0,0 +1,65 @@
import { Editor } from '../Editor'
import { TextSelection } from 'prosemirror-state'
import sleep from '../utils/sleep'
import minMax from '../utils/minMax'
declare module '../Editor' {
interface Editor {
focus(position: any): Editor
}
}
interface ResolvedSelection {
from: number,
to: number,
}
type Position = 'start' | 'end' | number | null
function resolveSelection(editor: Editor, position: Position = null): ResolvedSelection {
if (position === null) {
return editor.selection
}
if (position === 'start') {
return {
from: 0,
to: 0,
}
}
if (position === 'end') {
const { size } = editor.state.doc.content
return {
from: size,
to: size,
}
}
return {
from: position as number,
to: position as number,
}
}
export default async function focus(next: Function, editor: Editor, position: Position = null): Promise<void> {
const { view, state } = editor
if ((view.hasFocus && position === null)) {
next()
return
}
const { from, to } = resolveSelection(editor, position)
const { doc, tr } = state
const resolvedFrom = minMax(from, 0, doc.content.size)
const resolvedEnd = minMax(to, 0, doc.content.size)
const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd)
const transaction = tr.setSelection(selection)
view.dispatch(transaction)
await sleep(10)
view.focus()
next()
}

View File

@@ -0,0 +1,12 @@
import { Editor } from '../Editor'
declare module "../Editor" {
interface Editor {
insertText(text: string): Editor,
}
}
export default function insertText(next: Function, { state, view }: Editor, text: string): void {
view.dispatch(state.tr.insertText(text))
next()
}

View File

@@ -0,0 +1,3 @@
export default function minMax(value:number = 0, min:number = 0, max:number = 0): number {
return Math.min(Math.max(value, min), max)
}

View File

@@ -0,0 +1,3 @@
export default function sleep(milliseconds: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}