Skip to content

Commit

Permalink
Fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
curran committed Mar 6, 2023
1 parent e59f14e commit 320d6d3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
34 changes: 17 additions & 17 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module.exports = {
'env': {
'browser': true,
'es6': true,
'node': true
},
'extends': 'eslint:recommended',
'globals': {
'Atomics': 'readonly',
'SharedArrayBuffer': 'readonly'
},
'parserOptions': {
'ecmaVersion': 2018,
'sourceType': 'module'
},
'rules': {
'no-undef': 'off'
}
env: {
browser: true,
es6: true,
node: true,
},
extends: 'eslint:recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
rules: {
'no-undef': 'off',
},
};
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
24 changes: 21 additions & 3 deletions src/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { canOpAffectPath } from './canOpAffectPath';
// https://codemirror.net/6/examples/collab/
// https://github.com/yjs/y-codemirror.next/blob/main/src/y-sync.js#L107
// https://github.com/vizhub-core/vizhub/blob/main/vizhub-v2/packages/neoFrontend/src/pages/VizPage/Body/Editor/CodeEditor/CodeArea/CodeAreaCodeMirror5/index.js
export const json1Sync = ({ shareDBDoc, path = [], debug = false }) =>
export const json1Sync = ({
shareDBDoc,
path = [],
json1,
textUnicode,
debug = false,
}) =>
ViewPlugin.fromClass(
class {
// ShareDB --> CodeMirror
Expand Down Expand Up @@ -57,12 +63,24 @@ export const json1Sync = ({ shareDBDoc, path = [], debug = false }) =>
console.log(
' generated json1 op: ' +
JSON.stringify(
changesToOpJSON1(path, update.changes, update.startState.doc)
changesToOpJSON1(
path,
update.changes,
update.startState.doc,
json1,
textUnicode
)
)
);
}
shareDBDoc.submitOp(
changesToOpJSON1(path, update.changes, update.startState.doc)
changesToOpJSON1(
path,
update.changes,
update.startState.doc,
json1,
textUnicode
)
);
this.lock = false;
}
Expand Down
5 changes: 1 addition & 4 deletions src/translation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import json1 from 'ot-json1';
import textUnicode from 'ot-text-unicode';

// This module is able to translate from CodeMirror ChangeSet to OT ops
// and back, for both json0 and json1 OT types.
//
Expand Down Expand Up @@ -37,7 +34,7 @@ export const changesToOpJSON0 = (path, changeSet, doc) => {
};

// Converts a CodeMirror ChangeSet to a json1 OT op.
export const changesToOpJSON1 = (path, changeSet, doc) => {
export const changesToOpJSON1 = (path, changeSet, doc, json1, textUnicode) => {
const unicodeOp = [];

// Iterate over all changes in the ChangeSet.
Expand Down
11 changes: 7 additions & 4 deletions test/testIntegration.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as assert from 'assert';
import { json1Sync, canOpAffectPath } from '../src/index';
import json1 from 'ot-json1';
import textUnicode from 'ot-text-unicode';
import { EditorState, ChangeSet } from '@codemirror/state';
import { EditorView, ViewPlugin } from '@codemirror/view';
import { JSDOM } from 'jsdom';

import json1 from 'ot-json1';
import ShareDB from 'sharedb';
import { json1Sync, canOpAffectPath } from '../src/index';

ShareDB.types.register(json1.type);

Expand Down Expand Up @@ -34,7 +34,10 @@ const createEditor = ({ shareDBDoc, path, additionalExtensions = [] }) => {
const view = new EditorView({
state: EditorState.create({
doc: getAtPath(shareDBDoc, path),
extensions: [json1Sync({ shareDBDoc, path }), ...additionalExtensions],
extensions: [
json1Sync({ shareDBDoc, path, json1, textUnicode }),
...additionalExtensions,
],
}),
});
return view;
Expand Down
5 changes: 4 additions & 1 deletion test/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export const verify = (options) => {
it('changesToOpJSON1', () => {
const state = EditorState.create({ doc: atPath(before, path) });
const changeSet = ChangeSet.of(changes, before.length);
assert.deepEqual(changesToOpJSON1(path, changeSet, state.doc), opJSON1);
assert.deepEqual(
changesToOpJSON1(path, changeSet, state.doc, json1, textUnicode),
opJSON1
);
});

it('applied changes should match expected text', () => {
Expand Down

0 comments on commit 320d6d3

Please sign in to comment.