From 581264c5e342a58657020559702d9fd43202fa2b Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Fri, 29 Sep 2017 22:33:28 +0200 Subject: [PATCH] implement relative position helper --- examples/html-editor/index.js | 3 +-- src/Utils.js | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/examples/html-editor/index.js b/examples/html-editor/index.js index b5df0eea..db4bd6ff 100644 --- a/examples/html-editor/index.js +++ b/examples/html-editor/index.js @@ -7,8 +7,7 @@ Y({ }, connector: { name: 'websockets-client', - // url: 'http://127.0.0.1:1234', - url: 'http://192.168.178.81:1234', + url: 'http://127.0.0.1:1234', room: 'html-editor-example6' }, share: { diff --git a/src/Utils.js b/src/Utils.js index a80f3808..823353b4 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -49,6 +49,51 @@ export default function Utils (Y) { } } + Y.utils.getRelativePosition = function (type, offset) { + if (type == null) { + return null + } else { + if (type._content.length <= offset) { + return ['endof', type._model[0], type._model[1]] + } else { + return type._content[offset].id + } + } + } + + Y.utils.fromRelativePosition = function (y, id) { + var offset = 0 + var op + if (id[0] === 'endof') { + id = y.db.os.find(id.slice(1)).end + op = y.db.os.findNodeWithUpperBound(id).val + if (!op.deleted) { + offset = op.content != null ? op.content.length : 1 + } + } else { + op = y.db.os.findNodeWithUpperBound(id).val + if (!op.deleted) { + offset = id[1] - op.id[1] + } + } + + var type = y.db.getType(op.parent) + if (type == null || y.db.os.find(op.parent).deleted) { + return null + } + + while (op.left != null) { + op = y.db.os.findNodeWithUpperBound(op.left).val + if (!op.deleted) { + offset += op.content != null ? op.content.length : 1 + } + } + return { + type: type, + offset: offset + } + } + class NamedEventHandler { constructor () { this._eventListener = {}