wordinweb 0.2.0 → 0.2.2

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.
@@ -20533,8 +20533,14 @@ function setPageLayout(doc, patch, target) {
20533
20533
  return key ? elm.attrs[key] : void 0;
20534
20534
  };
20535
20535
  if (patch.size) {
20536
- setAttr(pgSz, "w", String(Math.round(patch.size.width * TWIPS_PER_INCH)));
20537
- setAttr(pgSz, "h", String(Math.round(patch.size.height * TWIPS_PER_INCH)));
20536
+ let width = Math.round(patch.size.width * TWIPS_PER_INCH);
20537
+ let height = Math.round(patch.size.height * TWIPS_PER_INCH);
20538
+ const currentWidth = parseInt(getAttr(pgSz, "w") ?? "12240", 10);
20539
+ const currentHeight = parseInt(getAttr(pgSz, "h") ?? "15840", 10);
20540
+ const isLandscape = getAttr(pgSz, "orient") === "landscape" || currentWidth > currentHeight;
20541
+ if (isLandscape !== width > height) [width, height] = [height, width];
20542
+ setAttr(pgSz, "w", String(width));
20543
+ setAttr(pgSz, "h", String(height));
20538
20544
  }
20539
20545
  if (patch.orientation) {
20540
20546
  const cw = parseInt(getAttr(pgSz, "w") ?? "12240", 10);
@@ -34206,6 +34212,7 @@ function WordArtMenu({ api }) {
34206
34212
  }
34207
34213
  function ChartMenu({ api, label = "Chart" }) {
34208
34214
  const [open, setOpen] = useState2(false);
34215
+ const [editing, setEditing] = useState2(false);
34209
34216
  const [type, setType] = useState2("column");
34210
34217
  const [title, setTitle] = useState2("");
34211
34218
  const [categories, setCategories] = useState2(["", ""]);
@@ -34222,16 +34229,35 @@ function ChartMenu({ api, label = "Chart" }) {
34222
34229
  }, [open]);
34223
34230
  const submit = () => {
34224
34231
  const categoryValues = categories.map((value) => value.trim());
34225
- const rawSeries = series.map((entry) => ({
34232
+ const rawSeries = (type === "pie" ? series.slice(0, 1) : series).map((entry) => ({
34226
34233
  name: entry.name.trim(),
34227
34234
  values: entry.values.map((value) => value.trim())
34228
34235
  }));
34229
- if (categoryValues.some((value) => !value) || rawSeries.some((entry) => !entry.name || entry.values.some((value) => value === "" || !Number.isFinite(Number(value))))) {
34230
- setError("Give every category and series a name, and enter a number in every data cell.");
34236
+ if (categoryValues.some((value) => !value)) {
34237
+ setError(type === "pie" ? "Enter a label for every slice." : "Enter a name for every category.");
34238
+ return;
34239
+ }
34240
+ if (type !== "pie" && rawSeries.some((entry) => !entry.name)) {
34241
+ setError("Enter a name for every series.");
34242
+ return;
34243
+ }
34244
+ if (rawSeries.some((entry) => entry.values.some((value) => value === "" || !Number.isFinite(Number(value))))) {
34245
+ setError("Enter a number in every value field.");
34246
+ return;
34247
+ }
34248
+ if (type === "pie" && rawSeries.some((entry) => entry.values.some((value) => Number(value) < 0))) {
34249
+ setError("Pie chart values must be zero or greater.");
34250
+ return;
34251
+ }
34252
+ if (type === "pie" && rawSeries.every((entry) => entry.values.every((value) => Number(value) === 0))) {
34253
+ setError("Enter at least one pie chart value greater than zero.");
34231
34254
  return;
34232
34255
  }
34233
- const seriesValues = rawSeries.map((entry) => ({ name: entry.name, values: entry.values.map(Number) }));
34234
- const data = { type, title, categories: categoryValues, series: seriesValues };
34256
+ const seriesValues = rawSeries.map((entry) => ({
34257
+ name: type === "pie" ? entry.name || "Values" : entry.name,
34258
+ values: entry.values.map(Number)
34259
+ }));
34260
+ const data = { type, title: title.trim(), categories: categoryValues, series: seriesValues };
34235
34261
  if (api?.updateSelectedChart(data) || api?.insertChart(data)) {
34236
34262
  setError("");
34237
34263
  setOpen(false);
@@ -34246,97 +34272,187 @@ function ChartMenu({ api, label = "Chart" }) {
34246
34272
  setCategories(categories.filter((_, itemIndex) => itemIndex !== index));
34247
34273
  setSeries(series.map((entry) => ({ ...entry, values: entry.values.filter((_, itemIndex) => itemIndex !== index) })));
34248
34274
  };
34275
+ const changeType = (next) => {
34276
+ setType(next);
34277
+ if (next === "pie") {
34278
+ setSeries((current) => [
34279
+ current[0] ?? { name: "Values", values: categories.map(() => "") }
34280
+ ]);
34281
+ }
34282
+ setError("");
34283
+ };
34249
34284
  const anchor = open ? rootRef.current?.getBoundingClientRect() : null;
34250
34285
  const viewportWidth = typeof window === "undefined" ? 456 : window.innerWidth;
34251
34286
  const popoverWidth = Math.min(440, viewportWidth - 16);
34252
34287
  const popoverLeft = Math.max(8, Math.min(anchor?.left ?? 8, viewportWidth - popoverWidth - 8));
34253
34288
  const fieldStyle = { width: "100%", boxSizing: "border-box", border: `1px solid ${T.border}`, borderRadius: 6, padding: "6px 8px", font: "13px system-ui, sans-serif", color: T.fg, background: T.popoverBg };
34289
+ const fieldLabelStyle = { display: "grid", gap: 3, color: T.muted, font: "11px system-ui, sans-serif" };
34290
+ const tableHeaderStyle = { padding: "0 3px 4px", textAlign: "left", verticalAlign: "bottom", color: T.muted, font: "600 11px system-ui, sans-serif" };
34291
+ const tableCellStyle = { padding: 3, verticalAlign: "bottom" };
34254
34292
  const toggle = () => {
34255
34293
  if (open) {
34256
34294
  setOpen(false);
34257
34295
  return;
34258
34296
  }
34259
34297
  const selected = api?.getSelectedChart();
34298
+ const nextCategories = selected?.categories.length ? [...selected.categories] : ["", ""];
34299
+ const nextSeries = selected?.series.length ? selected.series.map((entry) => ({
34300
+ name: entry.name,
34301
+ values: nextCategories.map((_, index) => String(entry.values[index] ?? ""))
34302
+ })) : [{ name: "", values: nextCategories.map(() => "") }];
34303
+ setEditing(!!selected);
34260
34304
  setType(selected?.type ?? "column");
34261
34305
  setTitle(selected?.title ?? "");
34262
- setCategories(selected ? [...selected.categories] : ["", ""]);
34263
- setSeries(selected ? selected.series.map((entry) => ({ name: entry.name, values: entry.values.map(String) })) : [{ name: "", values: ["", ""] }]);
34306
+ setCategories(nextCategories);
34307
+ setSeries(selected?.type === "pie" ? nextSeries.slice(0, 1) : nextSeries);
34264
34308
  setError("");
34265
34309
  setOpen(true);
34266
34310
  };
34311
+ const chartTypes = [
34312
+ { value: "column", label: "Column" },
34313
+ { value: "bar", label: "Bar" },
34314
+ { value: "line", label: "Line" },
34315
+ { value: "pie", label: "Pie" }
34316
+ ];
34267
34317
  return /* @__PURE__ */ jsxs2("span", { ref: rootRef, style: { position: "relative", display: "inline-block" }, children: [
34268
34318
  /* @__PURE__ */ jsx2("button", { title: "Insert or edit chart", style: btnStyle(open), onMouseDown: (event) => event.preventDefault(), onClick: toggle, children: label }),
34269
34319
  open && /* @__PURE__ */ jsxs2("div", { style: { position: "fixed", top: anchor?.bottom ?? 28, left: popoverLeft, zIndex: 100, width: popoverWidth, maxHeight: "calc(100vh - 48px)", overflow: "auto", boxSizing: "border-box", padding: 10, display: "grid", gap: 8, background: T.popoverBg, border: `1px solid ${T.border}`, borderRadius: 8, boxShadow: T.popoverShadow }, children: [
34270
- /* @__PURE__ */ jsx2(
34271
- ToolbarMenuSelect,
34272
- {
34273
- value: type,
34274
- ariaLabel: "Chart type",
34275
- width: "100%",
34276
- menuWidth: 300,
34277
- options: [
34278
- { value: "column", label: "Column" },
34279
- { value: "bar", label: "Bar" },
34280
- { value: "line", label: "Line" },
34281
- { value: "pie", label: "Pie" }
34282
- ],
34283
- onChange: (value) => setType(value),
34284
- style: fieldStyle
34285
- }
34286
- ),
34287
- /* @__PURE__ */ jsx2("input", { "aria-label": "Chart title", value: title, onChange: (event) => setTitle(event.target.value), placeholder: "Chart title", style: fieldStyle }),
34320
+ /* @__PURE__ */ jsx2("strong", { style: { color: T.fg, font: "600 13px system-ui, sans-serif" }, children: editing ? "Edit chart" : "Insert chart" }),
34321
+ /* @__PURE__ */ jsxs2("fieldset", { style: { minWidth: 0, margin: 0, padding: 0, border: 0 }, children: [
34322
+ /* @__PURE__ */ jsx2("legend", { style: { marginBottom: 4, color: T.muted, font: "11px system-ui, sans-serif" }, children: "Chart type" }),
34323
+ /* @__PURE__ */ jsx2("div", { role: "radiogroup", "aria-label": "Chart type", style: { display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 5 }, children: chartTypes.map((chartType) => {
34324
+ const selected = chartType.value === type;
34325
+ return /* @__PURE__ */ jsx2(
34326
+ "button",
34327
+ {
34328
+ type: "button",
34329
+ role: "radio",
34330
+ "aria-checked": selected,
34331
+ onClick: () => changeType(chartType.value),
34332
+ style: {
34333
+ minHeight: 34,
34334
+ border: `1px solid ${selected ? T.accent : T.border}`,
34335
+ borderRadius: 6,
34336
+ background: selected ? T.tabActiveBg : T.popoverBg,
34337
+ color: selected ? T.accent : T.fg,
34338
+ cursor: "pointer",
34339
+ font: "600 12px system-ui, sans-serif"
34340
+ },
34341
+ children: chartType.label
34342
+ },
34343
+ chartType.value
34344
+ );
34345
+ }) })
34346
+ ] }),
34347
+ /* @__PURE__ */ jsxs2("label", { style: fieldLabelStyle, children: [
34348
+ /* @__PURE__ */ jsx2("span", { children: "Chart title (optional)" }),
34349
+ /* @__PURE__ */ jsx2("input", { "aria-label": "Chart title", value: title, onChange: (event) => setTitle(event.target.value), style: fieldStyle })
34350
+ ] }),
34288
34351
  /* @__PURE__ */ jsxs2("div", { role: "group", "aria-label": "Chart data", style: { display: "grid", gap: 7 }, children: [
34289
- /* @__PURE__ */ jsx2("strong", { style: { color: T.fg, font: "600 11.5px system-ui, sans-serif" }, children: "Data" }),
34290
- /* @__PURE__ */ jsx2("div", { style: { overflowX: "auto" }, children: /* @__PURE__ */ jsxs2("div", { style: { display: "grid", gridTemplateColumns: `minmax(110px,1.2fr) repeat(${categories.length},minmax(72px,1fr)) 62px`, gap: 5, minWidth: categories.length > 3 ? 430 : void 0 }, children: [
34291
- /* @__PURE__ */ jsx2("span", { style: { alignSelf: "center", color: T.muted, font: "11px system-ui, sans-serif" }, children: "Series" }),
34292
- categories.map((category, index) => /* @__PURE__ */ jsxs2("label", { style: { display: "grid", gridTemplateColumns: "1fr auto", gap: 3 }, children: [
34293
- /* @__PURE__ */ jsx2(
34294
- "input",
34295
- {
34296
- "aria-label": `Chart category ${index + 1}`,
34297
- value: category,
34298
- placeholder: "Category",
34299
- onChange: (event) => setCategories(categories.map((value, itemIndex) => itemIndex === index ? event.target.value : value)),
34300
- style: fieldStyle
34301
- }
34302
- ),
34303
- categories.length > 1 && /* @__PURE__ */ jsx2("button", { type: "button", "aria-label": `Remove chart category ${index + 1}`, onClick: () => removeCategory(index), style: { ...pillBtn, padding: "0 6px" }, children: "\xD7" })
34304
- ] }, index)),
34305
- /* @__PURE__ */ jsx2("span", {}),
34306
- series.map((entry, seriesIndex) => /* @__PURE__ */ jsxs2(Fragment, { children: [
34307
- /* @__PURE__ */ jsx2(
34352
+ /* @__PURE__ */ jsx2("strong", { style: { color: T.fg, font: "600 11.5px system-ui, sans-serif" }, children: type === "pie" ? "Slices" : "Chart data" }),
34353
+ /* @__PURE__ */ jsx2("div", { style: { overflowX: "auto" }, children: type === "pie" ? /* @__PURE__ */ jsxs2("table", { "aria-label": "Pie chart data", style: { width: "100%", borderCollapse: "collapse" }, children: [
34354
+ /* @__PURE__ */ jsx2("thead", { children: /* @__PURE__ */ jsxs2("tr", { children: [
34355
+ /* @__PURE__ */ jsx2("th", { scope: "col", style: tableHeaderStyle, children: "Slice label" }),
34356
+ /* @__PURE__ */ jsx2("th", { scope: "col", style: tableHeaderStyle, children: "Value" }),
34357
+ /* @__PURE__ */ jsx2("th", { scope: "col", "aria-label": "Actions" })
34358
+ ] }) }),
34359
+ /* @__PURE__ */ jsx2("tbody", { children: categories.map((category, index) => /* @__PURE__ */ jsxs2("tr", { children: [
34360
+ /* @__PURE__ */ jsx2("th", { scope: "row", style: { ...tableCellStyle, width: "52%", fontWeight: 400 }, children: /* @__PURE__ */ jsxs2("label", { style: fieldLabelStyle, children: [
34361
+ /* @__PURE__ */ jsxs2("span", { children: [
34362
+ "Slice ",
34363
+ index + 1
34364
+ ] }),
34365
+ /* @__PURE__ */ jsx2(
34366
+ "input",
34367
+ {
34368
+ "aria-label": `Chart slice ${index + 1} label`,
34369
+ value: category,
34370
+ onChange: (event) => setCategories(categories.map((value, itemIndex) => itemIndex === index ? event.target.value : value)),
34371
+ style: fieldStyle
34372
+ }
34373
+ )
34374
+ ] }) }),
34375
+ /* @__PURE__ */ jsx2("td", { style: tableCellStyle, children: /* @__PURE__ */ jsx2(
34308
34376
  "input",
34309
34377
  {
34310
- "aria-label": `Chart series ${seriesIndex + 1} name`,
34311
- value: entry.name,
34312
- placeholder: "Series",
34313
- onChange: (event) => setSeries(series.map((value, itemIndex) => itemIndex === seriesIndex ? { ...value, name: event.target.value } : value)),
34378
+ "aria-label": `Chart slice ${index + 1} value`,
34379
+ type: "number",
34380
+ min: "0",
34381
+ step: "any",
34382
+ value: series[0]?.values[index] ?? "",
34383
+ onChange: (event) => setSeries([{
34384
+ ...series[0] ?? { name: "Values", values: categories.map(() => "") },
34385
+ values: categories.map((_, itemIndex) => itemIndex === index ? event.target.value : series[0]?.values[itemIndex] ?? "")
34386
+ }]),
34314
34387
  style: fieldStyle
34315
34388
  }
34316
- ),
34317
- entry.values.map((value, valueIndex) => /* @__PURE__ */ jsx2(
34389
+ ) }),
34390
+ /* @__PURE__ */ jsx2("td", { style: tableCellStyle, children: categories.length > 1 && /* @__PURE__ */ jsx2("button", { type: "button", "aria-label": `Remove chart slice ${index + 1}`, onClick: () => removeCategory(index), style: { ...pillBtn, padding: "0 7px" }, children: "Remove" }) })
34391
+ ] }, index)) })
34392
+ ] }) : /* @__PURE__ */ jsxs2("table", { "aria-label": `${type} chart data`, style: { minWidth: series.length > 2 ? 430 : void 0, width: "100%", borderCollapse: "collapse" }, children: [
34393
+ /* @__PURE__ */ jsx2("thead", { children: /* @__PURE__ */ jsxs2("tr", { children: [
34394
+ /* @__PURE__ */ jsx2("th", { scope: "col", style: tableHeaderStyle, children: "Category" }),
34395
+ series.map((entry, seriesIndex) => /* @__PURE__ */ jsxs2("th", { scope: "col", style: tableHeaderStyle, children: [
34396
+ /* @__PURE__ */ jsxs2("label", { style: fieldLabelStyle, children: [
34397
+ /* @__PURE__ */ jsxs2("span", { children: [
34398
+ "Series ",
34399
+ seriesIndex + 1,
34400
+ " name"
34401
+ ] }),
34402
+ /* @__PURE__ */ jsx2(
34403
+ "input",
34404
+ {
34405
+ "aria-label": `Chart series ${seriesIndex + 1} name`,
34406
+ value: entry.name,
34407
+ onChange: (event) => setSeries(series.map((value, itemIndex) => itemIndex === seriesIndex ? { ...value, name: event.target.value } : value)),
34408
+ style: fieldStyle
34409
+ }
34410
+ )
34411
+ ] }),
34412
+ series.length > 1 && /* @__PURE__ */ jsx2("button", { type: "button", "aria-label": `Remove chart series ${seriesIndex + 1}`, onClick: () => setSeries(series.filter((_, itemIndex) => itemIndex !== seriesIndex)), style: { ...pillBtn, marginTop: 3, padding: "0 7px" }, children: "Remove" })
34413
+ ] }, seriesIndex)),
34414
+ /* @__PURE__ */ jsx2("th", { scope: "col", "aria-label": "Actions" })
34415
+ ] }) }),
34416
+ /* @__PURE__ */ jsx2("tbody", { children: categories.map((category, categoryIndex) => /* @__PURE__ */ jsxs2("tr", { children: [
34417
+ /* @__PURE__ */ jsx2("th", { scope: "row", style: { ...tableCellStyle, minWidth: 120, fontWeight: 400 }, children: /* @__PURE__ */ jsxs2("label", { style: fieldLabelStyle, children: [
34418
+ /* @__PURE__ */ jsxs2("span", { children: [
34419
+ "Category ",
34420
+ categoryIndex + 1
34421
+ ] }),
34422
+ /* @__PURE__ */ jsx2(
34423
+ "input",
34424
+ {
34425
+ "aria-label": `Chart category ${categoryIndex + 1}`,
34426
+ value: category,
34427
+ onChange: (event) => setCategories(categories.map((value, itemIndex) => itemIndex === categoryIndex ? event.target.value : value)),
34428
+ style: fieldStyle
34429
+ }
34430
+ )
34431
+ ] }) }),
34432
+ series.map((entry, seriesIndex) => /* @__PURE__ */ jsx2("td", { style: { ...tableCellStyle, minWidth: 82 }, children: /* @__PURE__ */ jsx2(
34318
34433
  "input",
34319
34434
  {
34320
- "aria-label": `Chart series ${seriesIndex + 1} value ${valueIndex + 1}`,
34435
+ "aria-label": `Chart series ${seriesIndex + 1} value ${categoryIndex + 1}`,
34321
34436
  type: "number",
34322
34437
  step: "any",
34323
- value,
34324
- placeholder: "0",
34325
- onChange: (event) => setSeries(series.map((seriesValue, itemIndex) => itemIndex === seriesIndex ? { ...seriesValue, values: seriesValue.values.map((itemValue, itemValueIndex) => itemValueIndex === valueIndex ? event.target.value : itemValue) } : seriesValue)),
34438
+ value: entry.values[categoryIndex] ?? "",
34439
+ onChange: (event) => setSeries(series.map((seriesValue, itemIndex) => itemIndex === seriesIndex ? {
34440
+ ...seriesValue,
34441
+ values: categories.map((_, itemValueIndex) => itemValueIndex === categoryIndex ? event.target.value : seriesValue.values[itemValueIndex] ?? "")
34442
+ } : seriesValue)),
34326
34443
  style: fieldStyle
34327
- },
34328
- valueIndex
34329
- )),
34330
- /* @__PURE__ */ jsx2("button", { type: "button", "aria-label": `Remove chart series ${seriesIndex + 1}`, disabled: series.length === 1, onClick: () => setSeries(series.filter((_, itemIndex) => itemIndex !== seriesIndex)), style: { ...pillBtn, padding: "0 7px" }, children: "Remove" })
34331
- ] }, seriesIndex))
34444
+ }
34445
+ ) }, seriesIndex)),
34446
+ /* @__PURE__ */ jsx2("td", { style: tableCellStyle, children: categories.length > 1 && /* @__PURE__ */ jsx2("button", { type: "button", "aria-label": `Remove chart category ${categoryIndex + 1}`, onClick: () => removeCategory(categoryIndex), style: { ...pillBtn, padding: "0 7px" }, children: "Remove" }) })
34447
+ ] }, categoryIndex)) })
34332
34448
  ] }) }),
34333
34449
  /* @__PURE__ */ jsxs2("div", { style: { display: "flex", gap: 6 }, children: [
34334
- /* @__PURE__ */ jsx2("button", { type: "button", onClick: () => setSeries([...series, { name: "", values: categories.map(() => "") }]), style: { ...pillBtn, background: T.popoverBg, color: T.fg }, children: "Add series" }),
34335
- /* @__PURE__ */ jsx2("button", { type: "button", onClick: addCategory, style: { ...pillBtn, background: T.popoverBg, color: T.fg }, children: "Add category" })
34450
+ type !== "pie" && /* @__PURE__ */ jsx2("button", { type: "button", onClick: () => setSeries([...series, { name: "", values: categories.map(() => "") }]), style: { ...pillBtn, background: T.popoverBg, color: T.fg }, children: "Add series" }),
34451
+ /* @__PURE__ */ jsx2("button", { type: "button", onClick: addCategory, style: { ...pillBtn, background: T.popoverBg, color: T.fg }, children: type === "pie" ? "Add slice" : "Add category" })
34336
34452
  ] })
34337
34453
  ] }),
34338
34454
  error && /* @__PURE__ */ jsx2("div", { role: "alert", style: { color: "#c5221f", font: "11.5px system-ui, sans-serif" }, children: error }),
34339
- /* @__PURE__ */ jsx2("button", { onClick: submit, style: { border: 0, borderRadius: 6, padding: "7px 10px", background: T.accent, color: T.accentFg, cursor: "pointer", font: "600 12px system-ui, sans-serif" }, children: "Insert or update chart" })
34455
+ /* @__PURE__ */ jsx2("button", { onClick: submit, style: { border: 0, borderRadius: 6, padding: "7px 10px", background: T.accent, color: T.accentFg, cursor: "pointer", font: "600 12px system-ui, sans-serif" }, children: editing ? "Update chart" : "Insert chart" })
34340
34456
  ] })
34341
34457
  ] });
34342
34458
  }
@@ -38205,4 +38321,4 @@ export {
38205
38321
  cfb/cfb.js:
38206
38322
  (*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com *)
38207
38323
  */
38208
- //# sourceMappingURL=chunk-XGTWJMDG.js.map
38324
+ //# sourceMappingURL=chunk-JGXXZTQE.js.map