@sofereditor/core 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +132 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +158 -2
- package/dist/index.d.ts +158 -2
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
ANSWER_LINE_COLOR: () => ANSWER_LINE_COLOR,
|
|
34
|
+
ANSWER_LINE_MAX: () => ANSWER_LINE_MAX,
|
|
35
|
+
BLANK_MIN_RUN: () => BLANK_MIN_RUN,
|
|
36
|
+
BLANK_STYLE: () => BLANK_STYLE,
|
|
33
37
|
BLOCK_SPECS: () => BLOCK_SPECS,
|
|
34
38
|
CLEAR_ALL_MARKS: () => CLEAR_ALL_MARKS,
|
|
35
39
|
COMMAND_ORIGIN: () => COMMAND_ORIGIN,
|
|
@@ -40,8 +44,13 @@ __export(index_exports, {
|
|
|
40
44
|
MAX_LIST_LEVEL: () => MAX_LIST_LEVEL,
|
|
41
45
|
PAGE_PRESETS: () => PAGE_PRESETS,
|
|
42
46
|
SOFER_MIME: () => SOFER_MIME,
|
|
47
|
+
TABLE_BORDER_COLOR: () => TABLE_BORDER_COLOR,
|
|
48
|
+
TABLE_GUIDE_COLOR: () => TABLE_GUIDE_COLOR,
|
|
43
49
|
TRACKED_ORIGINS: () => TRACKED_ORIGINS,
|
|
44
50
|
Y: () => Y3,
|
|
51
|
+
answerLineStyle: () => answerLineStyle,
|
|
52
|
+
cellBorderColors: () => cellBorderColors,
|
|
53
|
+
cellBorderStyle: () => cellBorderStyle,
|
|
45
54
|
cellsInRect: () => cellsInRect,
|
|
46
55
|
clearRectCells: () => clearRectCells,
|
|
47
56
|
collapsedSelection: () => collapsedSelection,
|
|
@@ -65,6 +74,7 @@ __export(index_exports, {
|
|
|
65
74
|
getBlockId: () => getBlockId,
|
|
66
75
|
getMarksInRange: () => getMarksInRange,
|
|
67
76
|
indentList: () => indentList,
|
|
77
|
+
insertAnswerLines: () => insertAnswerLines,
|
|
68
78
|
insertImage: () => insertImage,
|
|
69
79
|
insertParagraph: () => insertParagraph,
|
|
70
80
|
insertSlice: () => insertSlice,
|
|
@@ -93,6 +103,7 @@ __export(index_exports, {
|
|
|
93
103
|
sameTextRun: () => sameTextRun,
|
|
94
104
|
serializeSelection: () => serializeSelection,
|
|
95
105
|
setBlockAttr: () => setBlockAttr,
|
|
106
|
+
setBlockAttrAtIndex: () => setBlockAttrAtIndex,
|
|
96
107
|
setBlockType: () => setBlockType,
|
|
97
108
|
setCellAttr: () => setCellAttr,
|
|
98
109
|
setColumnWidth: () => setColumnWidth,
|
|
@@ -104,6 +115,8 @@ __export(index_exports, {
|
|
|
104
115
|
spanOf: () => spanOf,
|
|
105
116
|
splitCell: () => splitCell,
|
|
106
117
|
splitListItem: () => splitListItem,
|
|
118
|
+
splitUnderscoreRuns: () => splitUnderscoreRuns,
|
|
119
|
+
styleToCssText: () => styleToCssText,
|
|
107
120
|
tableLocationOf: () => tableLocationOf,
|
|
108
121
|
tableRectSelection: () => tableRectSelection,
|
|
109
122
|
toggleList: () => toggleList,
|
|
@@ -705,6 +718,7 @@ var ALL_MARKS = {
|
|
|
705
718
|
underline: true,
|
|
706
719
|
strike: true,
|
|
707
720
|
color: true,
|
|
721
|
+
highlight: true,
|
|
708
722
|
fontFamily: true,
|
|
709
723
|
fontSize: true,
|
|
710
724
|
link: true,
|
|
@@ -836,6 +850,88 @@ function sameMarkValue(a, b) {
|
|
|
836
850
|
return false;
|
|
837
851
|
}
|
|
838
852
|
|
|
853
|
+
// src/decorations.ts
|
|
854
|
+
var BLANK_MIN_RUN = 3;
|
|
855
|
+
function splitUnderscoreRuns(text) {
|
|
856
|
+
if (text.length === 0) return [];
|
|
857
|
+
const re = new RegExp(`_{${BLANK_MIN_RUN},}`, "g");
|
|
858
|
+
const out = [];
|
|
859
|
+
let last = 0;
|
|
860
|
+
let match;
|
|
861
|
+
while ((match = re.exec(text)) !== null) {
|
|
862
|
+
if (match.index > last) out.push({ text: text.slice(last, match.index), blank: false });
|
|
863
|
+
out.push({ text: match[0], blank: true });
|
|
864
|
+
last = match.index + match[0].length;
|
|
865
|
+
}
|
|
866
|
+
if (last < text.length) out.push({ text: text.slice(last), blank: false });
|
|
867
|
+
return out;
|
|
868
|
+
}
|
|
869
|
+
var BLANK_STYLE = {
|
|
870
|
+
WebkitTextFillColor: "transparent",
|
|
871
|
+
borderBottom: "1px solid currentColor"
|
|
872
|
+
};
|
|
873
|
+
var ANSWER_LINE_COLOR = "#000000";
|
|
874
|
+
function answerLineStyle(attrs) {
|
|
875
|
+
if (attrs.answerLine !== true) return void 0;
|
|
876
|
+
const spacing = attrs.answerLineSpacing ?? 1;
|
|
877
|
+
return {
|
|
878
|
+
borderBottom: `1px solid ${ANSWER_LINE_COLOR}`,
|
|
879
|
+
lineHeight: String(spacing),
|
|
880
|
+
// `minHeight` casado com a entrelinha, e NÃO redundante: o CSS do
|
|
881
|
+
// consumidor costuma trazer `min-height` em parágrafo (o playground usa
|
|
882
|
+
// 1.5em "para manter parágrafo vazio visível"). Sem esta linha, "Simples"
|
|
883
|
+
// e "1,5" renderizariam com a mesma altura.
|
|
884
|
+
minHeight: `${spacing}em`
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
function styleToCssText(style) {
|
|
888
|
+
return Object.entries(style).map(([k, v]) => `${kebab(k)}:${v}`).join(";");
|
|
889
|
+
}
|
|
890
|
+
function kebab(prop) {
|
|
891
|
+
if (prop.startsWith("--")) return prop;
|
|
892
|
+
return prop.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// src/table-borders.ts
|
|
896
|
+
var TABLE_BORDER_COLOR = "#cbd5e1";
|
|
897
|
+
var TABLE_GUIDE_COLOR = "var(--ed-guide-color, transparent)";
|
|
898
|
+
function cellBorderColors(preset, pos, variant) {
|
|
899
|
+
const on = TABLE_BORDER_COLOR;
|
|
900
|
+
const off = variant === "screen" ? TABLE_GUIDE_COLOR : "transparent";
|
|
901
|
+
switch (preset ?? "all") {
|
|
902
|
+
case "horizontal":
|
|
903
|
+
return { top: on, right: off, bottom: on, left: off };
|
|
904
|
+
case "vertical":
|
|
905
|
+
return { top: off, right: on, bottom: off, left: on };
|
|
906
|
+
case "none":
|
|
907
|
+
return { top: off, right: off, bottom: off, left: off };
|
|
908
|
+
case "outer": {
|
|
909
|
+
const isTop = pos.row === pos.rowStart;
|
|
910
|
+
const isBottom = pos.row + Math.max(1, pos.rowspan) - 1 === pos.rowEnd - 1;
|
|
911
|
+
const isLeft = pos.col === 0;
|
|
912
|
+
const isRight = pos.col + Math.max(1, pos.colspan) - 1 === pos.cols - 1;
|
|
913
|
+
return {
|
|
914
|
+
top: isTop ? on : off,
|
|
915
|
+
right: isRight ? on : off,
|
|
916
|
+
bottom: isBottom ? on : off,
|
|
917
|
+
left: isLeft ? on : off
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
case "all":
|
|
921
|
+
default:
|
|
922
|
+
return { top: on, right: on, bottom: on, left: on };
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
function cellBorderStyle(preset, pos, variant) {
|
|
926
|
+
const c = cellBorderColors(preset, pos, variant);
|
|
927
|
+
return {
|
|
928
|
+
borderTopColor: c.top,
|
|
929
|
+
borderRightColor: c.right,
|
|
930
|
+
borderBottomColor: c.bottom,
|
|
931
|
+
borderLeftColor: c.left
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
|
|
839
935
|
// src/commands.ts
|
|
840
936
|
var Y3 = __toESM(require("yjs"), 1);
|
|
841
937
|
|
|
@@ -1160,6 +1256,16 @@ function setBlockAttr(ctx, key, value) {
|
|
|
1160
1256
|
ctx.setSelection(sel);
|
|
1161
1257
|
});
|
|
1162
1258
|
}
|
|
1259
|
+
function setBlockAttrAtIndex(ctx, blockIndex, key, value) {
|
|
1260
|
+
transact(ctx.doc, () => {
|
|
1261
|
+
const block = ctx.doc.getBlock(blockIndex);
|
|
1262
|
+
if (!block) return;
|
|
1263
|
+
const attrsMap = block.get("attrs") ?? new Y3.Map();
|
|
1264
|
+
if (!block.get("attrs")) block.set("attrs", attrsMap);
|
|
1265
|
+
if (value === null || value === void 0) attrsMap.delete(key);
|
|
1266
|
+
else attrsMap.set(key, value);
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1163
1269
|
function setCellAttr(ctx, key, value) {
|
|
1164
1270
|
transact(ctx.doc, () => {
|
|
1165
1271
|
const sel = ctx.getSelection();
|
|
@@ -1428,6 +1534,19 @@ function clampLevel(l) {
|
|
|
1428
1534
|
if (typeof l !== "number" || !Number.isFinite(l)) return 0;
|
|
1429
1535
|
return Math.max(0, Math.min(MAX_LIST_LEVEL, Math.trunc(l)));
|
|
1430
1536
|
}
|
|
1537
|
+
var ANSWER_LINE_MAX = 50;
|
|
1538
|
+
function insertAnswerLines(ctx, count, spacing) {
|
|
1539
|
+
const n = Math.max(1, Math.min(ANSWER_LINE_MAX, Math.trunc(count)));
|
|
1540
|
+
transact(ctx.doc, () => {
|
|
1541
|
+
const sel = ctx.getSelection();
|
|
1542
|
+
const focusBlock = Math.max(0, Math.min(sel.focus.blockIndex, ctx.doc.blockCount() - 1));
|
|
1543
|
+
const insertAt = focusBlock + 1;
|
|
1544
|
+
const attrs = { answerLine: true, answerLineSpacing: spacing };
|
|
1545
|
+
const blocks = Array.from({ length: n }, () => createBlock("paragraph", "", attrs));
|
|
1546
|
+
ctx.doc.blocks.insert(insertAt, blocks);
|
|
1547
|
+
ctx.setSelection(collapsedSelection({ blockIndex: insertAt, offset: 0 }));
|
|
1548
|
+
});
|
|
1549
|
+
}
|
|
1431
1550
|
function insertTable(ctx, rows, cols) {
|
|
1432
1551
|
const r = Math.max(1, Math.trunc(rows));
|
|
1433
1552
|
const c = Math.max(1, Math.trunc(cols));
|
|
@@ -2176,6 +2295,10 @@ var EditorHistory = class {
|
|
|
2176
2295
|
};
|
|
2177
2296
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2178
2297
|
0 && (module.exports = {
|
|
2298
|
+
ANSWER_LINE_COLOR,
|
|
2299
|
+
ANSWER_LINE_MAX,
|
|
2300
|
+
BLANK_MIN_RUN,
|
|
2301
|
+
BLANK_STYLE,
|
|
2179
2302
|
BLOCK_SPECS,
|
|
2180
2303
|
CLEAR_ALL_MARKS,
|
|
2181
2304
|
COMMAND_ORIGIN,
|
|
@@ -2186,8 +2309,13 @@ var EditorHistory = class {
|
|
|
2186
2309
|
MAX_LIST_LEVEL,
|
|
2187
2310
|
PAGE_PRESETS,
|
|
2188
2311
|
SOFER_MIME,
|
|
2312
|
+
TABLE_BORDER_COLOR,
|
|
2313
|
+
TABLE_GUIDE_COLOR,
|
|
2189
2314
|
TRACKED_ORIGINS,
|
|
2190
2315
|
Y,
|
|
2316
|
+
answerLineStyle,
|
|
2317
|
+
cellBorderColors,
|
|
2318
|
+
cellBorderStyle,
|
|
2191
2319
|
cellsInRect,
|
|
2192
2320
|
clearRectCells,
|
|
2193
2321
|
collapsedSelection,
|
|
@@ -2211,6 +2339,7 @@ var EditorHistory = class {
|
|
|
2211
2339
|
getBlockId,
|
|
2212
2340
|
getMarksInRange,
|
|
2213
2341
|
indentList,
|
|
2342
|
+
insertAnswerLines,
|
|
2214
2343
|
insertImage,
|
|
2215
2344
|
insertParagraph,
|
|
2216
2345
|
insertSlice,
|
|
@@ -2239,6 +2368,7 @@ var EditorHistory = class {
|
|
|
2239
2368
|
sameTextRun,
|
|
2240
2369
|
serializeSelection,
|
|
2241
2370
|
setBlockAttr,
|
|
2371
|
+
setBlockAttrAtIndex,
|
|
2242
2372
|
setBlockType,
|
|
2243
2373
|
setCellAttr,
|
|
2244
2374
|
setColumnWidth,
|
|
@@ -2250,6 +2380,8 @@ var EditorHistory = class {
|
|
|
2250
2380
|
spanOf,
|
|
2251
2381
|
splitCell,
|
|
2252
2382
|
splitListItem,
|
|
2383
|
+
splitUnderscoreRuns,
|
|
2384
|
+
styleToCssText,
|
|
2253
2385
|
tableLocationOf,
|
|
2254
2386
|
tableRectSelection,
|
|
2255
2387
|
toggleList,
|