mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
feat: update to slate 0.58 && fix site build
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@slate-collaborative/bridge",
|
||||
"version": "0.5.0",
|
||||
"version": "0.6.0",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
@@ -17,16 +17,16 @@
|
||||
"author": "cudr",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"prepublishOnly": "yarn run build",
|
||||
"build": "yarn run build:types && yarn run build:js",
|
||||
"prepublishOnly": "yarn run build:module",
|
||||
"build:module": "yarn run build:types && yarn run build:js",
|
||||
"build:types": "tsc --emitDeclarationOnly",
|
||||
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
|
||||
"watch": "yarn build:js -w",
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"automerge": "^0.14.0",
|
||||
"slate": "^0.57.2",
|
||||
"automerge": "0.14.0",
|
||||
"slate": "0.58.0",
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -63,5 +63,6 @@
|
||||
"^.+\\.ts?$": "ts-jest"
|
||||
},
|
||||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$"
|
||||
}
|
||||
},
|
||||
"gitHead": "1a29cf0da2dc171c1fadd5c8e6eac2327b5b0241"
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user