@coco-vexel/coco-office-oojson 1.0.10 → 1.0.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coco-vexel/coco-office-oojson",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "license": "Apache-2.0",
5
5
  "description": "OOJSON to OOXML / Flat OPC conversion and writeback bridge.",
6
6
  "type": "module",
@@ -10,7 +10,7 @@
10
10
  "./browser-global": "./dist/oojson.global.js"
11
11
  },
12
12
  "dependencies": {
13
- "@coco-vexel/coco-office": "^1.0.10"
13
+ "@coco-vexel/coco-office": "^1.0.11"
14
14
  },
15
15
  "files": [
16
16
  "src",
@@ -4,6 +4,7 @@
4
4
  import { esc, attrStr } from "./xml.mjs";
5
5
  import { colorToOoxml, highlightToOoxml, autoRgbAttr } from "./color.mjs";
6
6
  import { extraAttr, onlyKeys } from "./extra.mjs";
7
+ import { tableWidthToOoxml } from "./table-width.mjs";
7
8
 
8
9
  function shdXml(shd) {
9
10
  if (!shd) return "";
@@ -35,6 +36,7 @@ function ppToXml(pPr) {
35
36
  x += `<w:ind${attrStr(a)}/>`;
36
37
  }
37
38
  if (pPr.jc != null) x += `<w:jc w:val="${pPr.jc}"/>`;
39
+ if (Number.isInteger(pPr.outlineLvl)) x += `<w:outlineLvl w:val="${pPr.outlineLvl}"/>`;
38
40
  return x; // 返回 pPr 内层(不含 <w:pPr> 包裹),由 paraToXml 拼上段落 mark rPr + oo:x 后统一包裹
39
41
  }
40
42
  function rpInner(rPr) {
@@ -272,7 +274,8 @@ function widthXml(tag, w) {
272
274
  if (!w) return "";
273
275
  const a = {};
274
276
  if (w.type != null) a["w:type"] = w.type;
275
- const v = w.w != null ? w.w : w.value; if (v != null) a["w:w"] = v;
277
+ const v = w.w ?? w.value;
278
+ if (v != null) a["w:w"] = tableWidthToOoxml(w.type, v);
276
279
  return `<w:${tag}${attrStr(a)}/>`;
277
280
  }
278
281
  function marXml(tag, m) {
@@ -285,13 +288,20 @@ function tblPrXml(t) {
285
288
  if (!t) return "<w:tblPr/>";
286
289
  let x = "";
287
290
  if (t.tblStyle != null) x += `<w:tblStyle w:val="${esc(t.tblStyle)}"/>`;
291
+ if (t.inline === false) {
292
+ const attrs = {};
293
+ for (const key of ["horzAnchor", "vertAnchor", "tblpXSpec", "tblpYSpec", "tblpX", "tblpY", "leftFromText", "rightFromText", "topFromText", "bottomFromText"]) {
294
+ if (t.tblpPr?.[key] != null) attrs["w:" + key] = t.tblpPr[key];
295
+ }
296
+ x += `<w:tblpPr${attrStr(attrs)}/>`;
297
+ }
288
298
  x += widthXml("tblW", t.tblW);
289
299
  if (t.jc != null) x += `<w:jc w:val="${t.jc}"/>`;
290
300
  x += bordersXml("tblBorders", t.tblBorders);
291
301
  x += marXml("tblCellMar", t.tblCellMar);
292
302
  if (t.tblLayout != null) x += `<w:tblLayout w:type="${typeof t.tblLayout === "object" ? t.tblLayout.type : t.tblLayout}"/>`;
293
303
  if (t.tblLook != null && typeof t.tblLook === "object") { const a = {}; for (const k of Object.keys(t.tblLook)) if (k !== "type") a["w:" + k] = t.tblLook[k] === true ? 1 : t.tblLook[k] === false ? 0 : t.tblLook[k]; x += `<w:tblLook${attrStr(a)}/>`; }
294
- // tblInd/tblOverlap/tblpPr/inline 等 → 走 oo:x 兜底(形态古怪/非 OOXML 标准,建模易错)
304
+ // Keep inactive positioning metadata as well, for JSON round-trips.
295
305
  return `<w:tblPr${extraAttr(t)}>${x}</w:tblPr>`;
296
306
  }
297
307
  function cellToXml(c) {
@@ -6,6 +6,7 @@ import { parseXml, kid, kidsOf } from "./xml.mjs";
6
6
  import { colorToJson, highlightToJson, mergeAutoRgb } from "./color.mjs";
7
7
  import { readExtra } from "./extra.mjs";
8
8
  import { ooxmlDrawingToJson } from "./ooxml-drawing-to-json.mjs";
9
+ import { tableWidthFromOoxml } from "./table-width.mjs";
9
10
 
10
11
  // 数值用 Number 而非 parseInt:ONLYOFFICE 宽度等可为小数(如 tcW.w=22.36),parseInt 会截断丢精度。
11
12
  const a2i = (v) => (v == null || v === "" ? null : Number(v));
@@ -50,6 +51,8 @@ function xmlPpToJson(pPrNode, gaps) {
50
51
  if (ind.attrs["w:right"]) o.ind.right = a2i(ind.attrs["w:right"]);
51
52
  if (ind.attrs["w:firstLine"]) o.ind.firstLine = a2i(ind.attrs["w:firstLine"]);
52
53
  }
54
+ const outline = a2i(g("w:outlineLvl")?.attrs["w:val"]);
55
+ if (Number.isInteger(outline) && outline >= 0 && outline <= 9) o.outlineLvl = outline;
53
56
  if (g("w:jc")) o.jc = g("w:jc").attrs["w:val"];
54
57
  return { ...readExtra(pPrNode), ...o };
55
58
  }
@@ -213,7 +216,8 @@ function xmlBorders(node) {
213
216
  }
214
217
  function xmlWidth(node) {
215
218
  if (!node) return undefined;
216
- return { type: node.attrs["w:type"] || "auto", w: a2i(node.attrs["w:w"]) || 0 };
219
+ const type = node.attrs["w:type"] || "auto";
220
+ return { type, w: tableWidthFromOoxml(type, node.attrs["w:w"]) };
217
221
  }
218
222
  function xmlMar(node) {
219
223
  if (!node) return undefined;
@@ -222,18 +226,33 @@ function xmlMar(node) {
222
226
  return Object.keys(o).length ? o : undefined;
223
227
  }
224
228
  function tblPrToJson(node, gaps) {
225
- const o = { type: "tablePr" };
229
+ const o = { type: "tablePr", ...readExtra(node) };
230
+ // FromJSON treats a missing inline flag as floating. Standard DOCX tables are inline.
231
+ o.inline ??= true;
226
232
  if (!node) return o;
227
233
  const g = (t) => kid(node, t);
228
234
  if (g("w:tblStyle")) o.tblStyle = g("w:tblStyle").attrs["w:val"];
229
235
  if (g("w:tblW")) o.tblW = xmlWidth(g("w:tblW"));
236
+ const outline = a2i(g("w:outlineLvl")?.attrs["w:val"]);
237
+ if (Number.isInteger(outline) && outline >= 0 && outline <= 9) o.outlineLvl = outline;
230
238
  if (g("w:jc")) o.jc = g("w:jc").attrs["w:val"];
231
239
  if (g("w:tblBorders")) o.tblBorders = xmlBorders(g("w:tblBorders"));
232
240
  if (g("w:tblCellMar")) o.tblCellMar = xmlMar(g("w:tblCellMar"));
233
241
  if (g("w:tblLayout")) o.tblLayout = g("w:tblLayout").attrs["w:type"];
234
242
  if (g("w:tblLook")) { const a = g("w:tblLook").attrs; o.tblLook = {}; for (const k of ["firstColumn", "firstRow", "lastColumn", "lastRow", "noHBand", "noVBand"]) if (a["w:" + k] != null) o.tblLook[k] = a["w:" + k] === "1" || a["w:" + k] === "true"; }
235
- // tblInd(数字而非宽度对象)/tblOverlap/tblpPr/inline 等 → 由 oo:x 兜底,不在此解析以免写错形态
236
- return { ...readExtra(node), ...o };
243
+ const position = g("w:tblpPr");
244
+ if (position) {
245
+ o.inline = false;
246
+ const props = { ...o.tblpPr };
247
+ for (const key of ["horzAnchor", "vertAnchor", "tblpXSpec", "tblpYSpec"]) {
248
+ if (position.attrs["w:" + key] != null) props[key] = position.attrs["w:" + key];
249
+ }
250
+ for (const key of ["tblpX", "tblpY", "leftFromText", "rightFromText", "topFromText", "bottomFromText"]) {
251
+ if (position.attrs["w:" + key] != null) props[key] = Number(position.attrs["w:" + key]);
252
+ }
253
+ o.tblpPr = props;
254
+ }
255
+ return o;
237
256
  }
238
257
  function tcPrToJson(node, gaps) {
239
258
  const o = { type: "tableCellPr" };
@@ -252,6 +271,8 @@ function trPrToJson(node, gaps) {
252
271
  if (!node) return o;
253
272
  const g = (t) => kid(node, t);
254
273
  if (g("w:tblHeader")) o.tblHeader = true;
274
+ const outline = a2i(g("w:outlineLvl")?.attrs["w:val"]);
275
+ if (Number.isInteger(outline) && outline >= 0 && outline <= 9) o.outlineLvl = outline;
255
276
  if (g("w:jc")) o.jc = g("w:jc").attrs["w:val"];
256
277
  if (g("w:trHeight")) o.trHeight = { val: a2i(g("w:trHeight").attrs["w:val"]), hRule: g("w:trHeight").attrs["w:hRule"] };
257
278
  return { ...readExtra(node), ...o };
@@ -4,6 +4,7 @@
4
4
  import { esc, attrStr } from "./xml.mjs";
5
5
  import { colorToOoxml, autoRgbAttr, highlightToOoxml } from "./color.mjs";
6
6
  import { extraAttr } from "./extra.mjs";
7
+ import { tableWidthToOoxml } from "./table-width.mjs";
7
8
 
8
9
  function shdXml(shd) {
9
10
  if (!shd) return "";
@@ -35,7 +36,7 @@ function widthXml(tag, w) {
35
36
  const a = {};
36
37
  if (w.type != null) a["w:type"] = w.type;
37
38
  const v = w.w != null ? w.w : w.value;
38
- if (v != null) a["w:w"] = v;
39
+ if (v != null) a["w:w"] = tableWidthToOoxml(w.type, v);
39
40
  return `<w:${tag}${attrStr(a)}${extraAttr(w, ["type", "w", "value"])}/>`;
40
41
  }
41
42
 
@@ -0,0 +1,10 @@
1
+ // ONLYOFFICE JSON uses percentage points. OOXML numeric pct widths use 1/50 of a percent.
2
+ export function tableWidthToOoxml(type, width) {
3
+ return type === "pct" ? Math.round(width * 50) : width;
4
+ }
5
+
6
+ export function tableWidthFromOoxml(type, width) {
7
+ if (type !== "pct") return Number(width) || 0;
8
+ if (width?.endsWith("%")) return Number(width.slice(0, -1)) || 0;
9
+ return (Number(width) || 0) / 50;
10
+ }