mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
Merge pull request #2 from hiveteams/code-cleanup
chore: code and type cleanup
This commit is contained in:
commit
19d5234111
@ -6,7 +6,7 @@ import text from './text'
|
||||
import { SyncValue } from '../model'
|
||||
import { toJS } from '../utils'
|
||||
|
||||
const setSelection = (doc: any) => doc
|
||||
const setSelection = (doc: SyncValue) => doc
|
||||
|
||||
const opType = {
|
||||
...text,
|
||||
|
@ -1,9 +1,13 @@
|
||||
import * as Automerge from 'automerge'
|
||||
import { CollabMap, CollabOperation } from '../model'
|
||||
|
||||
const createByType = (type: Automerge.CollectionType) =>
|
||||
type === 'map' ? {} : type === 'list' ? [] : ''
|
||||
|
||||
const opCreate = ({ obj, type }: Automerge.Diff, [map, ops]: any) => {
|
||||
const opCreate = (
|
||||
{ obj, type }: Automerge.Diff,
|
||||
[map, ops]: [CollabMap, CollabOperation[]]
|
||||
) => {
|
||||
map[obj] = createByType(type)
|
||||
|
||||
return [map, ops]
|
||||
|
@ -2,7 +2,8 @@ import * as Automerge from 'automerge'
|
||||
|
||||
import { toSlatePath, toJS } from '../utils'
|
||||
|
||||
import { SyncDoc } from '../model'
|
||||
import { CollabMap, CollabOperation, SyncDoc } from '../model'
|
||||
import { Operation } from 'slate'
|
||||
|
||||
const insertTextOp = ({ index, path, value }: Automerge.Diff) => () => ({
|
||||
type: 'insert_text',
|
||||
@ -13,9 +14,9 @@ const insertTextOp = ({ index, path, value }: Automerge.Diff) => () => ({
|
||||
|
||||
const insertNodeOp = (
|
||||
{ value, obj, index, path }: Automerge.Diff,
|
||||
doc: any
|
||||
) => (map: any) => {
|
||||
const ops: any = []
|
||||
doc: SyncDoc
|
||||
) => (map: CollabMap) => {
|
||||
const ops: Operation[] = []
|
||||
|
||||
const iterate = ({ children, ...json }: any, path: any) => {
|
||||
if (children && children.length === 1 && children[0].text === '') {
|
||||
@ -56,7 +57,11 @@ const insertByType = {
|
||||
list: insertNodeOp
|
||||
}
|
||||
|
||||
const opInsert = (op: Automerge.Diff, [map, ops]: any, doc: SyncDoc) => {
|
||||
const opInsert = (
|
||||
op: Automerge.Diff,
|
||||
[map, ops]: [CollabMap, CollabOperation[]],
|
||||
doc: SyncDoc
|
||||
) => {
|
||||
try {
|
||||
const { link, obj, path, index, type, value } = op
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
import * as Automerge from 'automerge'
|
||||
import { Element } from 'slate'
|
||||
import { Element, Operation } from 'slate'
|
||||
|
||||
import { toSlatePath, toJS } from '../utils'
|
||||
import { getTarget } from '../path'
|
||||
import { rootKey } from './constants'
|
||||
import { CollabMap, CollabOperation } from '../model'
|
||||
|
||||
const removeTextOp = (op: Automerge.Diff) => (map: any, doc: Element) => {
|
||||
const removeTextOp = (op: Automerge.Diff) => (map: CollabMap, doc: Element) => {
|
||||
const { index, path, obj } = op
|
||||
|
||||
const slatePath = toSlatePath(path).slice(0, path?.length)
|
||||
@ -59,7 +60,10 @@ const removeNodeOp = ({ index, obj, path }: Automerge.Diff) => (
|
||||
}
|
||||
}
|
||||
|
||||
const opRemove = (op: Automerge.Diff, [map, ops]: any) => {
|
||||
const opRemove = (
|
||||
op: Automerge.Diff,
|
||||
[map, ops]: [CollabMap, CollabOperation[]]
|
||||
) => {
|
||||
try {
|
||||
const { index, path, obj, type } = op
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
import * as Automerge from 'automerge'
|
||||
import { Operation, Node } from 'slate'
|
||||
import { Operation, Node, SetNodeOperation, Element } from 'slate'
|
||||
import { CollabMap, CollabOperation, SyncDoc } from '../model'
|
||||
|
||||
import { toSlatePath, toJS } from '../utils'
|
||||
import { rootKey } from './constants'
|
||||
|
||||
const setDataOp = (
|
||||
{ key = '', obj, path, value }: Automerge.Diff,
|
||||
doc: any
|
||||
) => (map: any) => {
|
||||
doc: SyncDoc
|
||||
) => (map: CollabMap) => {
|
||||
return {
|
||||
type: 'set_node',
|
||||
path: toSlatePath(path),
|
||||
@ -17,10 +18,17 @@ const setDataOp = (
|
||||
newProperties: {
|
||||
[key]: value
|
||||
}
|
||||
}
|
||||
} as SetNodeOperation
|
||||
}
|
||||
|
||||
const setChildren = (op: Automerge.Diff, doc: any) => (map: any) => {
|
||||
/**
|
||||
* Convert a root level children update to slate operations.
|
||||
*
|
||||
* When we receive a root level children array update we need to handle that
|
||||
* operation as a special case since slate does not allow for setting the root
|
||||
* level node through a simple set_node operation
|
||||
*/
|
||||
const setChildren = (op: Automerge.Diff) => (map: CollabMap, doc: Element) => {
|
||||
const { value } = op
|
||||
|
||||
const ops: Operation[] = []
|
||||
@ -52,15 +60,20 @@ const setChildren = (op: Automerge.Diff, doc: any) => (map: any) => {
|
||||
return ops
|
||||
}
|
||||
|
||||
const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
|
||||
const opSet = (
|
||||
op: Automerge.Diff,
|
||||
[map, ops]: [CollabMap, CollabOperation[]],
|
||||
doc: SyncDoc
|
||||
) => {
|
||||
const { link, value, path, obj, key } = op
|
||||
|
||||
try {
|
||||
// Update our map to include the latest linked values if provided
|
||||
if (map[obj]) {
|
||||
map[obj][key as any] = link ? map[value] : value
|
||||
}
|
||||
|
||||
// ignore all cursor updates since those do not need to translate into
|
||||
// Ignore all cursor updates since those do not need to translate into
|
||||
// slate operations
|
||||
if (path && path.includes('cursors')) {
|
||||
return [map, ops]
|
||||
@ -68,8 +81,10 @@ const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
|
||||
|
||||
// Handle updates received for the root children array
|
||||
if (obj === rootKey && key === 'children') {
|
||||
ops.push(setChildren(op, doc))
|
||||
} else if (path && obj !== rootKey && !path.includes('cursors')) {
|
||||
ops.push(setChildren(op))
|
||||
}
|
||||
// Handle other setNode operations
|
||||
else if (path && obj !== rootKey && path.length !== 0) {
|
||||
ops.push(setDataOp(op, doc))
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Automerge from 'automerge'
|
||||
import { Node, Range } from 'slate'
|
||||
import { Element, Node, Operation, Range } from 'slate'
|
||||
|
||||
export type SyncValue = Automerge.List<Node>
|
||||
|
||||
@ -7,6 +7,14 @@ export type SyncDoc = Automerge.Doc<{ children: SyncValue; cursors: Cursors }>
|
||||
|
||||
export type CollabActionType = 'operation' | 'document'
|
||||
|
||||
export type CollabMap = { [key: string]: any }
|
||||
|
||||
export type CollabOperation =
|
||||
| Operation
|
||||
| Operation[]
|
||||
| ((map: CollabMap, doc: Element) => Operation)
|
||||
| ((map: CollabMap, doc: Element) => Operation[])
|
||||
|
||||
export interface CollabAction {
|
||||
type: CollabActionType
|
||||
payload: any
|
||||
|
@ -3,6 +3,7 @@ import * as Automerge from 'automerge'
|
||||
import { toSync } from '../'
|
||||
|
||||
import { Node } from 'slate'
|
||||
import { SyncDoc } from '../model'
|
||||
|
||||
export const createText = (text: string = '') => ({
|
||||
text
|
||||
@ -25,4 +26,4 @@ export const createValue = (children?: any): { children: Node[] } => ({
|
||||
export const createDoc = (children?: any) =>
|
||||
Automerge.from(toSync(createValue(children)))
|
||||
|
||||
export const cloneDoc = (doc: any) => Automerge.change(doc, '', d => d)
|
||||
export const cloneDoc = (doc: SyncDoc) => Automerge.change(doc, '', d => d)
|
||||
|
@ -2,7 +2,7 @@ import Automerge from 'automerge'
|
||||
import { createServer } from 'http'
|
||||
import fs from 'fs'
|
||||
import isEqual from 'lodash/isEqual'
|
||||
import { createEditor, Editor, Node, Transforms } from 'slate'
|
||||
import { createEditor, Editor, Element, Node, Transforms } from 'slate'
|
||||
import { createDoc, SyncDoc, toJS, toSlateOp } from '@hiveteams/collab-bridge'
|
||||
import AutomergeCollaboration from '@hiveteams/collab-backend/lib/AutomergeCollaboration'
|
||||
import withIOCollaboration from './withIOCollaboration'
|
||||
@ -259,7 +259,7 @@ describe('automerge editor client tests', () => {
|
||||
})
|
||||
|
||||
it('should not throw index error', () => {
|
||||
const doc: any = { children: [] }
|
||||
const doc: Element = { children: [] }
|
||||
const target = getTarget(doc, [0, 0])
|
||||
|
||||
expect(target).toEqual(null)
|
||||
|
Loading…
Reference in New Issue
Block a user