Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json0/ot-text string edits to ot-text-unicode json1 string edits #2

Open
curran opened this issue Sep 5, 2020 · 1 comment
Open

Comments

@curran
Copy link

curran commented Sep 5, 2020

A function that does this conversion would be great.

Has it been done somewhere already? Or is a completely new implementation required?

@highsea
Copy link

highsea commented Feb 23, 2024

Need to pay attention to modification:

  • Need to put text --> text-unicode
  • require ot-json1 and use these methods: type, removeOp, insertOp, replaceOp, editOp
const { type, removeOp, insertOp, replaceOp, editOp } = require('ot-json1');

...
...

function json0to1(json0Op) {
  // JSON0 ops are a list of {p: path, operation}.
  // See https://github.com/ottypes/json0#summary-of-operations
  return json0Op.map(c => {
    ...
    else if (c.t) return c.t === 'text0' ? editOp(c.p, 'text-unicode', text0ToText(c.o)) : editOp(c.p, c.t, c.o)
    // Note: Using the old text type here because thats what json0 uses.
    // You will have to register it if you want to use it.
    else if (c.si != null) return editOp(c.p.slice(0, -1), 'text-unicode', textOpAt(c.p[c.p.length - 1], c.si))
    else if (c.sd != null) return editOp(c.p.slice(0, -1), 'text-unicode', textOpAt(c.p[c.p.length - 1], { d: c.sd.length }))
    ...
    }).reduce(type.compose, null)
    ...

Test the final effect:

const json0to1 = require('./index')
console.log(json0to1([{ "p": ["content", 0], "si": "g" }]))
// [ 'content', { es: [ 'g' ] } ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants