2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
let EditorShareJsCodec
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
export default EditorShareJsCodec = {
|
2020-05-19 05:02:56 -04:00
|
|
|
rangeToShareJs(range, lines) {
|
|
|
|
let offset = 0
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
const line = lines[i]
|
|
|
|
offset += i < range.row ? line.length : range.column
|
|
|
|
}
|
|
|
|
offset += range.row // Include newlines
|
|
|
|
return offset
|
|
|
|
},
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
changeToShareJs(delta, lines) {
|
|
|
|
const offset = EditorShareJsCodec.rangeToShareJs(delta.start, lines)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const text = delta.lines.join('\n')
|
|
|
|
switch (delta.action) {
|
|
|
|
case 'insert':
|
|
|
|
return { i: text, p: offset }
|
|
|
|
case 'remove':
|
|
|
|
return { d: text, p: offset }
|
|
|
|
default:
|
|
|
|
throw new Error(`unknown action: ${delta.action}`)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
shareJsOffsetToRowColumn(offset, lines) {
|
|
|
|
let row = 0
|
|
|
|
for (row = 0; row < lines.length; row++) {
|
|
|
|
const line = lines[row]
|
|
|
|
if (offset <= line.length) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
offset -= lines[row].length + 1
|
|
|
|
} // + 1 for newline char
|
|
|
|
return { row, column: offset }
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-12-15 05:23:54 -05:00
|
|
|
}
|