feat: update to slate 0.58 && fix site build

This commit is contained in:
cudr
2020-05-11 09:21:49 +03:00
parent 0fd9390a99
commit 6adf4082dc
30 changed files with 180 additions and 107 deletions

View File

@@ -3,7 +3,7 @@ import { Operation } from 'slate'
import node from './node'
import text from './text'
import { SyncDoc } from '../model'
import { SyncValue } from '../model'
import { toJS } from '../utils'
const setSelection = (doc: any) => doc
@@ -14,7 +14,7 @@ const opType = {
set_selection: setSelection
}
const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
const applyOperation = (doc: SyncValue, op: Operation): SyncValue => {
try {
const applyOp = opType[op.type]
@@ -30,7 +30,7 @@ const applyOperation = (doc: SyncDoc, op: Operation): SyncDoc => {
}
}
const applySlateOps = (doc: SyncDoc, operations: Operation[]): SyncDoc => {
const applySlateOps = (doc: SyncValue, operations: Operation[]): SyncValue => {
return operations.reduce(applyOperation, doc)
}

View File

@@ -1,10 +1,10 @@
import { InsertNodeOperation } from 'slate'
import { SyncDoc } from '../../model'
import { SyncValue } from '../../model'
import { getParent, getChildren } from '../../path'
import { toSync } from '../../utils'
const insertNode = (doc: SyncDoc, op: InsertNodeOperation): SyncDoc => {
const insertNode = (doc: SyncValue, op: InsertNodeOperation): SyncValue => {
const [parent, index] = getParent(doc, op.path)
if (parent.text) {

View File

@@ -1,10 +1,10 @@
import { MergeNodeOperation, Node } from 'slate'
import { SyncDoc } from '../../model'
import { SyncValue } from '../../model'
import { getParent, getChildren } from '../../path'
import { toJS, cloneNode } from '../../utils'
const mergeNode = (doc: SyncDoc, op: MergeNodeOperation): SyncDoc => {
const mergeNode = (doc: SyncValue, op: MergeNodeOperation): SyncValue => {
const [parent, index]: [any, number] = getParent(doc, op.path)
const prev = parent[index - 1] || parent.children[index - 1]

View File

@@ -1,10 +1,10 @@
import { MoveNodeOperation } from 'slate'
import { cloneNode } from '../../utils'
import { SyncDoc } from '../../model'
import { SyncValue } from '../../model'
import { getParent, getChildren } from '../../path'
const moveNode = (doc: SyncDoc, op: MoveNodeOperation): SyncDoc => {
const moveNode = (doc: SyncValue, op: MoveNodeOperation): SyncValue => {
const [from, fromIndex] = getParent(doc, op.path)
const [to, toIndex] = getParent(doc, op.newPath)

View File

@@ -1,9 +1,12 @@
import { RemoveNodeOperation } from 'slate'
import { SyncDoc } from '../../model'
import { SyncValue } from '../../model'
import { getParent, getChildren } from '../../path'
export const removeNode = (doc: SyncDoc, op: RemoveNodeOperation): SyncDoc => {
export const removeNode = (
doc: SyncValue,
op: RemoveNodeOperation
): SyncValue => {
const [parent, index] = getParent(doc, op.path)
if (parent.text) {

View File

@@ -1,9 +1,9 @@
import { SetNodeOperation } from 'slate'
import { SyncDoc } from '../../model'
import { SyncValue } from '../../model'
import { getTarget } from '../../path'
const setNode = (doc: SyncDoc, op: SetNodeOperation): SyncDoc => {
const setNode = (doc: SyncValue, op: SetNodeOperation): SyncValue => {
const node = getTarget(doc, op.path)
const { newProperties } = op

View File

@@ -1,10 +1,10 @@
import { SplitNodeOperation } from 'slate'
import { SyncDoc } from '../../model'
import { SyncValue } from '../../model'
import { getParent, getChildren } from '../../path'
import { cloneNode } from '../../utils'
const splitNode = (doc: SyncDoc, op: SplitNodeOperation): SyncDoc => {
const splitNode = (doc: SyncValue, op: SplitNodeOperation): SyncValue => {
const [parent, index]: [any, number] = getParent(doc, op.path)
const target = getChildren(parent)[index]

View File

@@ -1,9 +1,12 @@
import { InsertTextOperation, RemoveTextOperation } from 'slate'
import { getTarget } from '../path'
import { SyncDoc } from '../model'
import { SyncValue } from '../model'
export const insertText = (doc: SyncDoc, op: InsertTextOperation): SyncDoc => {
export const insertText = (
doc: SyncValue,
op: InsertTextOperation
): SyncValue => {
const node = getTarget(doc, op.path)
node.text.insertAt(op.offset, ...op.text.split(''))
@@ -11,7 +14,10 @@ export const insertText = (doc: SyncDoc, op: InsertTextOperation): SyncDoc => {
return doc
}
export const removeText = (doc: SyncDoc, op: RemoveTextOperation): SyncDoc => {
export const removeText = (
doc: SyncValue,
op: RemoveTextOperation
): SyncValue => {
const node = getTarget(doc, op.path)
node.text.deleteAt(op.offset, op.text.length)

View File

@@ -1,11 +1,12 @@
import * as Automerge from 'automerge'
import { Node } from 'slate'
import opInsert from './insert'
import opRemove from './remove'
import opSet from './set'
import opCreate from './create'
import { SyncDoc } from '../model'
const byAction = {
create: opCreate,
remove: opRemove,
@@ -15,7 +16,7 @@ const byAction = {
const rootKey = '00000000-0000-0000-0000-000000000000'
const toSlateOp = (ops: Automerge.Diff[], doc: Automerge.Doc<Node>) => {
const toSlateOp = (ops: Automerge.Diff[], doc: SyncDoc) => {
const iterate = (acc: [any, any[]], op: Automerge.Diff): any => {
const action = byAction[op.action]

View File

@@ -16,17 +16,17 @@ export const setCursor = (
const newCursor = cursorOps[cursorOps.length - 1]?.newProperties || {}
if (selection) {
doc.cursors[id] = JSON.stringify(
Object.assign(
(doc.cursors[id] && JSON.parse(doc.cursors[id])) || {},
newCursor,
selection,
{
...cursorData,
isForward: Boolean(newCursor.focus)
}
)
const newCursorData = Object.assign(
(doc.cursors[id] && JSON.parse(doc.cursors[id])) || {},
newCursor,
selection,
{
...cursorData,
isForward: Range.isForward(selection)
}
)
doc.cursors[id] = JSON.stringify(newCursorData)
} else {
delete doc.cursors[id]
}

View File

@@ -1,7 +1,9 @@
import Automerge from 'automerge'
import { Node, Range } from 'slate'
export type SyncDoc = Automerge.Doc<Node & Cursors>
export type SyncValue = Automerge.List<Node[]>
export type SyncDoc = Automerge.Doc<{ children: SyncValue; cursors: Cursors }>
export type CollabActionType = 'operation' | 'document'
@@ -18,6 +20,6 @@ export interface Cursor extends Range, CursorData {
isForward: boolean
}
export interface Cursors {
export type Cursors = {
[key: string]: Cursor
}

View File

@@ -1,10 +1,10 @@
import { Node, Path } from 'slate'
import { SyncDoc } from '../model'
import { SyncValue } from '../model'
export const isTree = (node: Node): boolean => Boolean(node?.children)
export const getTarget = (doc: SyncDoc, path: Path) => {
export const getTarget = (doc: SyncValue, path: Path) => {
const iterate = (current: any, idx: number) => {
if (!(isTree(current) || current[idx])) {
throw new TypeError(
@@ -30,7 +30,7 @@ export const getParentPath = (
}
export const getParent = (
doc: SyncDoc,
doc: SyncValue,
path: Path,
level = 1
): [any, number] => {
@@ -39,4 +39,4 @@ export const getParent = (
return [getTarget(doc, parentPath), idx]
}
export const getChildren = (node: Node) => node.children || node
export const getChildren = (node: any) => node.children || node