Skip to content

feat(editor): convert text to table, table sorting, formula, and cell spacing - #29

Merged
DemoMacro merged 0 commit into
DemoMacro:mainfrom
rixzkiye:feat/table-power-tools
Sep 18, 2026
Merged

DemoMacro merged 0 commit into
DemoMacro:mainfrom
rixzkiye:feat/table-power-tools

Conversation

@rixzkiye

@rixzkiye rixzkiye commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This pull request implements the remaining Word-grade power tools for tables in Docen, completing the table layout and data capabilities:

  1. Convert Text to Table (convert-text-to-table):

    • The inverse operation of convert-to-text.
    • Delimiter auto-detection (\t, ,, ;, |).
    • Supports multiline and multiparagraph selections.
    • Pads rows to a uniform rectangular grid.
    • Preserves optional header row (tableHeader: true).
    • Un-disables "Convert Text to Table" in the Table dropdown ribbon (ribbon.ts).
  2. Table Sorting (sort-table and enhanced sort):

    • Sorts table rows by active column or explicit column index.
    • Protects header rows (tableHeader: true) from being sorted.
    • Natural numeric (1, 2, 10) and locale comparison in both ascending and descending order.
    • When inside a table, the global sort command seamlessly delegates to sort-table.
    • Added to Table Layout ribbon Data group.
  3. Table Formula (table-formula):

    • Evaluates Word table formulas: =SUM(ABOVE), =SUM(LEFT), =AVERAGE(...), =COUNT(...), =MIN(...), =MAX(...), =PRODUCT(...).
    • Supports cell references (A1:B3, A1+B1).
    • Auto-detects formula based on adjacent numbers (scanning above, then left).
    • Handles currency and formatted numbers (,500.50, 25%, (50)).
    • Uses a safe recursive-descent arithmetic evaluator.
    • Added to Table Layout ribbon Data group with Fluent mathFormula icon.
  4. Table Cell Spacing (table-cell-spacing and TablePropertiesPatch.cellSpacing):

    • Sets w:tblCellSpacing twips or universal measure strings ("10pt", "0.1cm") on the table node.
    • Supports clearing via 0 or null.
  5. Ribbon UI & i18n:

    • Registered mathFormula in RIBBON_ICONS.
    • Added Ribbon buttons for Sort and Formula in Table Layout Data group.
    • Added translations in English and Simplified Chinese for all new commands.

Test Coverage

  • Comprehensive unit tests in commands.spec.ts covering convert-text-to-table, sort-table, table-formula, and table-cell-spacing.
  • All 38 test suites passing (637 / 637 tests).
  • 0 lint or type errors (vp check --fix).

@DemoMacro

Copy link
Copy Markdown
Owner

Hi @rixzkiye — of the whole series, this is the PR we're most interested in. Convert-text-to-table (complementing our existing convert-to-text), table sort, table formulas, and cell spacing are all real gaps in main, and the direction is exactly right. I'd like to invite you to email me directly at abc@imst.xyz to talk through the details below — it'll be faster than PR comments.

What keeps us from merging as-is (all fixable):

  1. table-formula destroys cell contentreplaceWith on the whole cell replaces every paragraph and run style with one bare paragraph. Word's behavior is to replace the selection within the cell, preserving the rest (and its formatting).
  2. table-formula auto-pick dead branchif (hasAbove) … else if (hasLeft) … else "=SUM(ABOVE)" : the else returns the same thing as the hasAbove branch, so the hasLeft check never changes the outcome.
  3. sort-table breaks vMerge — rebuilding the whole table row-by-row doesn't carry vertically-merged cells; a table with vMerge comes out structurally corrupted. (Also: Word disables sort for merged tables — that's an honest precedent if you'd rather disable.)
  4. convert-text-to-table miscomputes ranges in nested structures$from.index(0) / hand-derived positions break when the selection crosses list/table boundaries; resolving through $from.blockRange() or similar keeps it inside the actual parent.
  5. The commit also rewrites merge-cells — our version carries span-aware selection fixes and cross-page vmerge handling that this rewrite predates; the four new commands should land without touching the existing merge implementation.

None of these are architectural — they're all inside the command layer we'd merge into. Fix these on top of current main (rebased) and we're ready to take it. Again: abc@imst.xyz if you'd like to discuss.

@rixzkiye
rixzkiye force-pushed the feat/table-power-tools branch from 5ce804b to 26cec9a Compare September 15, 2026 23:38
@rixzkiye

Copy link
Copy Markdown
Contributor Author

Hi @DemoMacro,

Following up on your review feedback, I've rebased this PR cleanly on latest main and addressed all 5 review points:

  1. table-formula (selection preservation): Replaced whole-cell overwrite with active selection replacement inside the cell (tr.insertText(resultText, from, to) / active paragraph targeting), preserving surrounding paragraphs, formatting, and cell styling.
  2. table-formula (auto-pick): Cleaned up the auto-pick condition to if (!hasAbove && hasLeft) formulaStr = "=SUM(LEFT)"; else formulaStr = "=SUM(ABOVE)";.
  3. sort-table (merged cells): Added guard to decline sorting (return false) when the table contains merged cells (verticalMerge or columnSpan > 1), matching Word's behavior.
  4. convert-text-to-table (scoping): Updated to use $from.blockRange($to) to properly respect container and parent boundaries.
  5. merge-cells: Left completely untouched, preserving latest main implementation.

All tests and typechecks pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants