rebuilt with correct sdk

This commit is contained in:
or-else
2024-05-18 18:07:14 -07:00
parent 944cf32bb9
commit ba81261ee8
9 changed files with 166 additions and 80 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+80 -22
View File
@@ -1220,7 +1220,7 @@ class DB {
/***/ ((module) => {
/**
* @copyright 2015-2022 Tinode LLC.
* @copyright 2015-2024 Tinode LLC.
* @summary Minimally rich text representation and formatting for Tinode.
* @license Apache 2.0
*
@@ -1280,6 +1280,7 @@ const MAX_PREVIEW_DATA_SIZE = 64;
const JSON_MIME_TYPE = 'application/json';
const DRAFTY_MIME_TYPE = 'text/x-drafty';
const ALLOWED_ENT_FIELDS = ['act', 'height', 'duration', 'incoming', 'mime', 'name', 'premime', 'preref', 'preview', 'ref', 'size', 'state', 'url', 'val', 'width'];
const segmenter = new Intl.Segmenter();
const INLINE_STYLES = [{
name: 'ST',
start: /(?:^|[\W_])(\*)[^\s*]/,
@@ -1700,24 +1701,48 @@ Drafty.parse = function (content) {
if (blx.length > 0) {
result.txt = blx[0].txt;
result.fmt = (blx[0].fmt || []).concat(blx[0].ent || []);
if (result.fmt.length) {
const segments = segmenter.segment(result.txt);
for (const ele of result.fmt) {
({
at: ele.at,
len: ele.len
} = toGraphemeValues(ele, segments, result.txt));
}
}
for (let i = 1; i < blx.length; i++) {
const block = blx[i];
const offset = result.txt.length + 1;
const offset = stringToGraphemes(result.txt).length + 1;
result.fmt.push({
tp: 'BR',
len: 1,
at: offset - 1
});
let segments = {};
result.txt += ' ' + block.txt;
if (block.fmt) {
segments = segmenter.segment(block.txt);
result.fmt = result.fmt.concat(block.fmt.map(s => {
s.at += offset;
const {
at: correctAt,
len: correctLen
} = toGraphemeValues(s, segments, block.txt);
s.at = correctAt + offset;
s.len = correctLen;
return s;
}));
}
if (block.ent) {
if (isEmptyObject(segments)) {
segments = segmenter.segment(block.txt);
}
result.fmt = result.fmt.concat(block.ent.map(s => {
s.at += offset;
const {
at: correctAt,
len: correctLen
} = toGraphemeValues(s, segments, block.txt);
s.at = correctAt + offset;
s.len = correctLen;
return s;
}));
}
@@ -1739,7 +1764,7 @@ Drafty.append = function (first, second) {
return first;
}
first.txt = first.txt || '';
const len = first.txt.length;
const len = stringToGraphemes(first.txt).length;
if (typeof second == 'string') {
first.txt += second;
} else if (second.txt) {
@@ -1927,7 +1952,7 @@ Drafty.quote = function (header, uid, body) {
const quote = Drafty.append(Drafty.appendLineBreak(Drafty.mention(header, uid)), body);
quote.fmt.push({
at: 0,
len: quote.txt.length,
len: stringToGraphemes(quote.txt).length,
tp: 'QQ'
});
return quote;
@@ -1937,7 +1962,7 @@ Drafty.mention = function (name, uid) {
txt: name || '',
fmt: [{
at: 0,
len: (name || '').length,
len: stringToGraphemes(name || '').length,
key: 0
}],
ent: [{
@@ -2101,7 +2126,7 @@ Drafty.appendLineBreak = function (content) {
};
content.fmt = content.fmt || [];
content.fmt.push({
at: content.txt.length,
at: stringToGraphemes(content.txt).length,
len: 1,
tp: 'BR'
});
@@ -2504,7 +2529,7 @@ function draftyToTree(doc) {
key: key
});
return;
} else if (at + len > txt.length) {
} else if (at + len > stringToGraphemes(txt).length) {
return;
}
if (!span.tp) {
@@ -2546,7 +2571,8 @@ function draftyToTree(doc) {
span.type = 'HD';
}
});
let tree = spansToTree({}, txt, 0, txt.length, spans);
const graphemes = stringToGraphemes(txt);
let tree = spansToTree({}, graphemes, 0, graphemes.length, spans);
const flatten = function (node) {
if (Array.isArray(node.children) && node.children.length == 1) {
const child = node.children[0];
@@ -2582,11 +2608,11 @@ function addNode(parent, n) {
parent.children.push(n);
return parent;
}
function spansToTree(parent, text, start, end, spans) {
function spansToTree(parent, graphemes, start, end, spans) {
if (!spans || spans.length == 0) {
if (start < end) {
addNode(parent, {
text: text.substring(start, end)
text: graphemes.slice(start, end).map(segment => segment.segment).join('')
});
}
return parent;
@@ -2604,7 +2630,7 @@ function spansToTree(parent, text, start, end, spans) {
}
if (start < span.start) {
addNode(parent, {
text: text.substring(start, span.start)
text: graphemes.slice(start, span.start).map(segment => segment.segment).join('')
});
start = span.start;
}
@@ -2629,12 +2655,12 @@ function spansToTree(parent, text, start, end, spans) {
type: span.type,
data: span.data,
key: span.key
}, text, start, span.end, subspans));
}, graphemes, start, span.end, subspans));
start = span.end;
}
if (start < end) {
addNode(parent, {
text: text.substring(start, end)
text: graphemes.slice(start, end).map(segment => segment.segment).join('')
});
}
return parent;
@@ -2644,7 +2670,7 @@ function treeToDrafty(doc, tree, keymap) {
return doc;
}
doc.txt = doc.txt || '';
const start = doc.txt.length;
const start = stringToGraphemes(doc.txt).length;
if (tree.text) {
doc.txt += tree.text;
} else if (Array.isArray(tree.children)) {
@@ -2653,7 +2679,7 @@ function treeToDrafty(doc, tree, keymap) {
});
}
if (tree.type) {
const len = doc.txt.length - start;
const len = stringToGraphemes(doc.txt).length - start;
doc.fmt = doc.fmt || [];
if (Object.keys(tree.data || {}).length > 0) {
doc.ent = doc.ent || [];
@@ -2755,12 +2781,12 @@ function shortenTree(tree, limit, tail) {
node.text = tail;
limit = -1;
} else if (node.text) {
const len = node.text.length;
if (len > limit) {
node.text = node.text.substring(0, limit) + tail;
const graphemes = stringToGraphemes(node.text);
if (graphemes.length > limit) {
node.text = graphemes.slice(0, limit).map(segment => segment.segment).join('') + tail;
limit = -1;
} else {
limit -= len;
limit -= graphemes.length;
}
}
return node;
@@ -2907,6 +2933,38 @@ function copyEntData(data, light, allow) {
}
return null;
}
function isEmptyObject(obj) {
return Object.keys(obj ?? {}).length == 0;
}
;
function graphemeIndices(graphemes) {
const result = [];
let graphemeIndex = 0;
let charIndex = 0;
for (const {
segment
} of graphemes) {
for (let i = 0; i < segment.length; i++) {
result[charIndex + i] = graphemeIndex;
}
charIndex += segment.length;
graphemeIndex++;
}
return result;
}
function toGraphemeValues(fmt, segments, txt) {
segments = segments ?? segmenter.segment(txt);
const indices = graphemeIndices(segments);
const correctAt = indices[fmt.at];
const correctLen = fmt.at + fmt.len <= txt.length ? indices[fmt.at + fmt.len - 1] - correctAt : fmt.len;
return {
at: correctAt,
len: correctLen + 1
};
}
function stringToGraphemes(str) {
return Array.from(segmenter.segment(str));
}
if (true) {
module.exports = Drafty;
}
@@ -4857,7 +4915,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ PACKAGE_VERSION: () => (/* binding */ PACKAGE_VERSION)
/* harmony export */ });
const PACKAGE_VERSION = "0.22.12";
const PACKAGE_VERSION = "0.22.13";
/***/ })
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long