Skip to content

Commit

Permalink
fix dependency update (#50)
Browse files Browse the repository at this point in the history
* Always call external onChange.

* Fix split_node missing properties bug.

* fix: dependency update build

Co-authored-by: Ulion <[email protected]>
  • Loading branch information
cudr and ulion authored Jan 18, 2021
1 parent 1a536f2 commit a679869
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/SocketIOConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class SocketIOCollaboration {
*/

constructor(options: SocketIOCollaborationOptions) {
this.io = io(options.entry, options.connectOpts)
this.io = io(options.entry as Server, options.connectOpts)

this.backend = new AutomergeBackend()

Expand Down
83 changes: 51 additions & 32 deletions packages/bridge/src/convert/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,70 @@ import { toSlatePath, toJS } from '../utils'
import { getTarget } from '../path'

const removeTextOp = (op: Automerge.Diff) => (map: any, doc: Element) => {
const { index, path, obj } = op
try {
const { index, path, obj } = op

const slatePath = toSlatePath(path).slice(0, path?.length)
const slatePath = toSlatePath(path).slice(0, path?.length)

let node
let node = map[obj]

try {
node = getTarget(doc, slatePath) || map[obj]
} catch (e) {
console.error(e, op, doc)
}
try {
node = getTarget(doc, slatePath)
} catch (e) {
console.error(e, slatePath, op, map, toJS(doc))
}

if (typeof index !== 'number') return
if (typeof index !== 'number') return

const text = node?.text[index] || '*'
const text = node?.text?.[index] || '*'

if (node) {
node.text = node.text?.slice(0, index) + node.text?.slice(index + 1)
}
if (node) {
node.text = node?.text ? (node.text.slice(0, index) + node.text.slice(index + 1)) : ''
}

return {
type: 'remove_text',
path: slatePath,
offset: index,
text,
marks: []
return {
type: 'remove_text',
path: slatePath,
offset: index,
text,
marks: []
}
} catch (e) {
console.error(e, op, map, toJS(doc))
}
}

const removeNodeOp = ({ index, obj, path }: Automerge.Diff) => (
const removeNodeOp = (op: Automerge.Diff) => (
map: any,
doc: Element
) => {
const slatePath = toSlatePath(path)
try {
const { index, obj, path } = op

const parent = getTarget(doc, slatePath)
const target = parent?.children[index as number] || { children: [] }
const slatePath = toSlatePath(path)

if (!map.hasOwnProperty(obj)) {
map[obj] = target
}
const parent = getTarget(doc, slatePath)
const target = parent?.children?.[index as number] || parent?.[index as number] || { children: [] }

if (!target) {
throw new TypeError('Target is not found!')
}

if (!map.hasOwnProperty(obj)) {
map[obj] = target
}

return {
type: 'remove_node',
path: slatePath.length ? slatePath.concat(index) : [index],
node: target
if (!Number.isInteger(index)) {
throw new TypeError('Index is not a number')
}

return {
type: 'remove_node',
path: slatePath.length ? slatePath.concat(index) : [index],
node: target
}
} catch (e) {
console.error(e, op, map, toJS(doc))
}
}

Expand All @@ -61,7 +79,8 @@ const opRemove = (op: Automerge.Diff, [map, ops]: any) => {
if (
map.hasOwnProperty(obj) &&
typeof map[obj] !== 'string' &&
type !== 'text'
type !== 'text' &&
map?.obj?.length
) {
map[obj].splice(index, 1)

Expand All @@ -72,7 +91,7 @@ const opRemove = (op: Automerge.Diff, [map, ops]: any) => {

const key = path[path.length - 1]

if (key === 'cursors') return [map, ops]
if (key === 'cursors' || op.key === 'cursors') return [map, ops]

const fn = key === 'text' ? removeTextOp : removeNodeOp

Expand Down
6 changes: 3 additions & 3 deletions packages/bridge/src/convert/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const setDataOp = (
type: 'set_node',
path: toSlatePath(path),
properties: {
[key]: Automerge.getObjectById(doc, obj)?.[key]
[key]: toJS(Automerge.getObjectById(doc, obj)?.[key])
},
newProperties: {
[key]: value
[key]: map?.[value] || value
}
}
}
Expand All @@ -22,7 +22,7 @@ const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
const { link, value, path, obj, key } = op

try {
if (path && path[0] !== 'cursors') {
if (path && path.length && path[0] !== 'cursors') {
ops.push(setDataOp(op, doc))
} else if (map[obj]) {
map[obj][key as any] = link ? map[value] : value
Expand Down
4 changes: 3 additions & 1 deletion packages/client/src/automerge-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ export const AutomergeEditor = {
Editor.withoutNormalizing(e, () => {
if (HistoryEditor.isHistoryEditor(e) && !preserveExternalHistory) {
HistoryEditor.withoutSaving(e, () => {
slateOps.forEach((o: Operation) => e.apply(o))
slateOps.forEach((o: Operation) => {
e.apply(o)
})
})
} else {
slateOps.forEach((o: Operation) => e.apply(o))
Expand Down
20 changes: 11 additions & 9 deletions packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@
"@emotion/styled": "^10.0.17",
"@slate-collaborative/backend": "^0.7.1",
"@slate-collaborative/client": "^0.7.0",
"@types/faker": "^4.1.5",
"@types/is-url": "^1.2.28",
"@types/jest": "24.0.18",
"@types/node": "14.14.6",
"@types/randomcolor": "^0.5.4",
"@types/react-dom": "^16.9.6",
"concurrently": "^4.1.2",
"cross-env": "^6.0.3",
"express": "^4.17.1",
"faker": "^5.1.0",
"is-url": "^1.2.4",
"lodash": "^4.17.15",
"nodemon": "^2.0.6",
Expand All @@ -26,8 +19,7 @@
"react-scripts": "3.1.2",
"slate": "0.59.0",
"slate-history": "0.58.3",
"slate-react": "0.59.0",
"typescript": "^3.8.3"
"slate-react": "0.59.0"
},
"scripts": {
"start": "nodemon server.js",
Expand All @@ -54,5 +46,15 @@
},
"engines": {
"node": "12.x"
},
"devDependencies": {
"@types/faker": "^4.1.5",
"@types/is-url": "^1.2.28",
"@types/jest": "24.0.18",
"@types/node": "14.14.6",
"@types/randomcolor": "^0.5.4",
"@types/react-dom": "^16.9.6",
"faker": "^5.1.0",
"typescript": "^3.8.3"
}
}

0 comments on commit a679869

Please sign in to comment.