From b99295c8bb4252f1490c633caf06ba317bd2c605 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Tue, 17 Nov 2026 13:38:06 -0500 Subject: [PATCH 1/2] Keep column moves within their group, add any property as a column, and fix Reset and Other Properties around a scrolling list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Columns menu follows the in-MATLAB data explorer's Columns dropdown: - A column moves only among the columns of its own group, in the menu and by its header. Over another group's item or header the drag shows no drop and a drop moves nothing. The table's columns are always group by group; an order saved before this is shown regrouped, each group in its saved order. - An Other Properties group takes any property name typed into its field (Enter or Add) and shows it as a column at the table's end. Its cells are the rows' _props of that name, empty on a row whose object has no such property. A built-in column's key or label shows that column instead; a name that is no MATLAB property name, or one already shown, is refused with a notice. Added columns are kept under the storageKey with their shown/hidden state, move among themselves only, are searched like the others, stay through Reset, and are removed by their ×. - The title and Reset sit at the top, Other Properties is fixed at the bottom, and only the built-in list between them scrolls. The menu is no taller than the space below the Columns button, with a 120 px floor. Rows carry _props: stampRow adds the object's own properties that the row has no column for, from a class object's property children and a Simulink object's property bag (text, numbers, logicals, lists of those and typed scalars, printed as core's Property Inspector prints them; nested objects and *_internal fields left out). It reads them directly rather than through toPIObject(), which cost three quarters of a 15,000-entry dictionary's open; with this, that open takes the same time. The goldens gain _props and nothing else: with the projection turned off, the previous snapshots pass 140 of 140. --- custom-elements.json | 4 +- dist/model/rows/propertyCells.d.ts | 2 + dist/model/rows/propertyCells.js | 102 +++ dist/model/rows/rowStamp.d.ts | 3 +- dist/model/rows/rowStamp.js | 9 +- dist/model/rows/tableRow.d.ts | 6 + dist/widgets/tree-table/dex-tree-table.d.ts | 24 +- dist/widgets/tree-table/dex-tree-table.js | 650 ++++++++++++++---- docs/widgets/tree-table.md | 5 +- src/model/rows/propertyCells.ts | 98 +++ src/model/rows/rowStamp.ts | 8 +- src/model/rows/tableRow.ts | 6 + src/widgets/tree-table/dex-tree-table.ts | 641 +++++++++++++---- test/columnMenuGroups.test.ts | 214 ++++++ .../table-lazy.json | 3 + .../table.json | 3 + .../table-lazy.json | 13 + .../table.json | 13 + .../children-lazy.json | 8 +- .../table-lazy.json | 8 +- .../table.json | 21 +- .../children-lazy.json | 4 +- .../table-lazy.json | 7 +- .../table.json | 13 +- .../children-lazy.json | 12 +- .../table-lazy.json | 2 +- .../table.json | 2 +- .../table-lazy.json | 6 + .../table.json | 6 + .../table-lazy.json | 9 + .../table.json | 9 + .../table-lazy.json | 9 + .../table.json | 9 + .../table-lazy.json | 24 + .../table.json | 2 +- .../table-lazy.json | 11 + .../table.json | 2 +- .../table-lazy.json | 12 + .../table.json | 2 +- .../table-lazy.json | 18 + .../table.json | 2 +- .../table-lazy.json | 40 ++ .../table.json | 40 ++ .../table-lazy.json | 5 + .../table.json | 5 + .../table-lazy.json | 5 + .../table.json | 5 + .../children-lazy.json | 6 + .../table-lazy.json | 42 ++ .../table.json | 47 +- .../children-lazy.json | 24 + .../test__fixtures__arch.sldd/table-lazy.json | 31 + .../test__fixtures__arch.sldd/table.json | 55 ++ .../children-lazy.json | 8 + .../table.json | 8 + .../table-lazy.json | 5 + .../table.json | 5 + .../table-lazy.json | 3 + .../test__fixtures__mcos__Bp.mat/table.json | 3 + .../table-lazy.json | 5 + .../test__fixtures__mcos__Lut.mat/table.json | 5 + .../table-lazy.json | 15 + .../table.json | 15 + .../table-lazy.json | 3 + .../table.json | 3 + .../table-lazy.json | 3 + .../table.json | 3 + .../table-lazy.json | 6 + .../test__fixtures__mcos__Sig.mat/table.json | 6 + .../table-lazy.json | 30 + .../test__fixtures__mcos__all.sldd/table.json | 30 + .../children-lazy.json | 30 + .../table-lazy.json | 18 + .../table.json | 48 ++ .../children-lazy.json | 10 + .../table-lazy.json | 11 + .../table.json | 21 + .../table-lazy.json | 46 ++ .../table.json | 46 ++ .../children-lazy.json | 9 + .../table-lazy.json | 12 + .../table.json | 21 + .../table-lazy.json | 9 + .../table.json | 9 + .../children-lazy.json | 4 +- .../table.json | 22 +- .../children-lazy.json | 6 + .../table.json | 6 + .../children-lazy.json | 8 + .../table.json | 8 + .../children-lazy.json | 8 + .../table.json | 8 + .../children-lazy.json | 9 + .../table-lazy.json | 12 + .../table.json | 21 + .../children-lazy.json | 9 + .../table-lazy.json | 11 + .../table.json | 20 + .../children-lazy.json | 4 + .../table-lazy.json | 6 + .../test__fixtures__rt_bin.sldd/table.json | 10 + .../children-lazy.json | 4 + .../table-lazy.json | 6 + .../test__fixtures__rt_text.sldd/table.json | 10 + .../children-lazy.json | 33 + .../table-lazy.json | 6 + .../table.json | 39 ++ .../table-lazy.json | 6 + .../table.json | 6 + test/propertyCells.test.ts | 133 ++++ test/treeTableColumns.test.ts | 13 +- test/treeTablePropertyColumns.test.ts | 263 +++++++ 112 files changed, 3131 insertions(+), 313 deletions(-) create mode 100644 dist/model/rows/propertyCells.d.ts create mode 100644 dist/model/rows/propertyCells.js create mode 100644 src/model/rows/propertyCells.ts create mode 100644 test/columnMenuGroups.test.ts create mode 100644 test/propertyCells.test.ts create mode 100644 test/treeTablePropertyColumns.test.ts diff --git a/custom-elements.json b/custom-elements.json index 067ef4e6..3c4f3bd7 100644 --- a/custom-elements.json +++ b/custom-elements.json @@ -8139,7 +8139,7 @@ "declarations": [ { "kind": "class", - "description": "The tree table: a document's rows as a tree, with a search bar, sortable, resizable and\nreorderable columns (the Columns menu shows, hides and orders them, and a right-click on a\nheader filters on that column), cell editors, row drag and drop, links, matrix glyphs, an\n`+ Add` gallery and virtual scrolling for very large documents. Data comes in by properties\n(`rows` above all) and every user action goes out as a `dex-*` event; it never changes the\ndocument itself. Its defaults are a full editor; `readonly`, `canAdd` and `rowDrag` turn parts\nof that off.", + "description": "The tree table: a document's rows as a tree, with a search bar, sortable, resizable and\nreorderable columns (the Columns menu shows, hides and orders them, each within its group, and\nadds a column for any property the user names, from the rows' `_props`; a right-click on a\nheader filters on that column), cell editors, row drag and drop, links, matrix glyphs, an\n`+ Add` gallery and virtual scrolling for very large documents. Data comes in by properties\n(`rows` above all) and every user action goes out as a `dex-*` event; it never changes the\ndocument itself. Its defaults are a full editor; `readonly`, `canAdd` and `rowDrag` turn parts\nof that off.", "name": "DexTreeTable", "members": [ { @@ -8187,7 +8187,7 @@ "text": "Record | null" }, "default": "null", - "description": "The group heading each column sits under in the Columns menu, by column key. Default null:\nno headings. Property only." + "description": "The group heading each column sits under in the Columns menu, by column key. The columns are\nshown group by group, and the user moves a column within its group only, in the menu and by\nits header. Default null: no headings. Property only." }, { "kind": "field", diff --git a/dist/model/rows/propertyCells.d.ts b/dist/model/rows/propertyCells.d.ts new file mode 100644 index 00000000..1c55fe0c --- /dev/null +++ b/dist/model/rows/propertyCells.d.ts @@ -0,0 +1,2 @@ +/** The cell text of each of the node's own properties the row has no column for, or undefined. */ +export declare function propertyCells(n: any, row: Record): Record | undefined; diff --git a/dist/model/rows/propertyCells.js b/dist/model/rows/propertyCells.js new file mode 100644 index 00000000..ef204b88 --- /dev/null +++ b/dist/model/rows/propertyCells.js @@ -0,0 +1,102 @@ +// Copyright 2026 The MathWorks, Inc. +// +// A row's properties beyond its columns: what a column the user names in the table's Columns menu +// (its Other Properties group) shows. The table reads the cell of such a column from the row's +// `_props`, by the property's name; a row whose object has no property of that name leaves the +// cell empty. +// +// Two places hold an object's own properties, and both are read: +// - an object whose CHILDREN are its class's properties (core's `isObjectPropertyBag`: a MATLAB +// class object, so a `MyClass` with `MyPropA`), each child by its name, as the child's own row +// shows its value; +// - an object that keeps a property bag (`serial._properties`, a Simulink object and a class +// derived from one), each top-level property by its name, where the value is one a property +// column shows: text, a number, a logical, a list of those, or a typed scalar (MATLAB's rule +// for these columns). Not a nested object (`CoderInfo`, `LoggingInfo`), whose own properties +// are not columns, nor a `*_internal` or envelope key. +// +// A property the row already has a cell for (Value, Description, `complexity` for `Complexity`) +// is left out: the menu shows that column for its name instead, so its text here would be a +// second copy of the cell on every row. +// +// The values are printed as core's Property Inspector prints them in its Other group (core's +// piOther), for the shapes kept; core does not export that printer, and calling `toPIObject()` +// per row for it cost three quarters of a 15,000-entry dictionary's open (measured 2026-11-17). +/** The cell text of each of the node's own properties the row has no column for, or undefined. */ +export function propertyCells(n, row) { + let props; + const put = (name, text) => { + if (inRow(row, name) || (props && name in props)) + return; + (props ??= {})[name] = text; + }; + let isBag = false; + try { + isBag = n?.isObjectPropertyBag === true; + } + catch { + isBag = false; + } + if (isBag && Array.isArray(n.children)) { + for (const child of n.children) { + const name = child?.name; + // An object array's children are its elements (`obj(1)`), not properties. + if (typeof name !== 'string' || !PROPERTY_NAME.test(name)) + continue; + let text; + try { + text = child.displayValue; + } + catch { + continue; + } + put(name, typeof text === 'string' ? text : text === undefined || text === null ? '' : String(text)); + } + } + const bag = n?.serial?._properties; + if (isPlainObject(bag)) { + for (const name of Object.keys(bag)) { + if (!PROPERTY_NAME.test(name) || name.endsWith('_internal')) + continue; + const text = primitiveText(bag[name]); + if (text !== null) + put(name, text); + } + } + return props; +} +// A MATLAB property name: a letter, then letters, digits and underscores. +const PROPERTY_NAME = /^[A-Za-z][A-Za-z0-9_]*$/; +// Whether the row has a cell for `name`: under its own key, or under the lower-camel key a schema +// column gives a property (`Complexity` → `complexity`). +function inRow(row, name) { + return name in row || name.charAt(0).toLowerCase() + name.slice(1) in row; +} +function isPlainObject(v) { + return typeof v === 'object' && v !== null && !Array.isArray(v); +} +// The text of a value a property column shows, or null for one it does not. Each kept shape is +// printed as core's piOther prints it: a scalar as String(), a list as `[a, b, c]`, a typed +// scalar as its `_value`. The typed scalars core reads before printing (complex or MAT-stream +// cdata, an encoded binary value, a sparse array, a `Matrix(…)` empty) are not kept. +function primitiveText(v) { + if (typeof v === 'string') + return v; + if (typeof v === 'number' || typeof v === 'boolean') + return String(v); + if (Array.isArray(v)) { + return v.every((x) => typeof x === 'string' || typeof x === 'number' || typeof x === 'boolean') + ? '[' + v.join(', ') + ']' + : null; + } + if (isPlainObject(v) && '_type' in v && '_value' in v) { + const value = v._value; + if (v._type === 'cdata' || v._type === 'sparse' || '_attrs' in v) + return null; + if (typeof value === 'number' || typeof value === 'boolean') + return String(value); + if (typeof value === 'string' && !value.startsWith('Matrix(')) + return value; + } + return null; +} diff --git a/dist/model/rows/rowStamp.d.ts b/dist/model/rows/rowStamp.d.ts index 68d2ad95..5600012c 100644 --- a/dist/model/rows/rowStamp.d.ts +++ b/dist/model/rows/rowStamp.d.ts @@ -89,7 +89,8 @@ export interface StampContext { * In this order, which both builders already applied: the entry's Status and clip mark, then * the capability flags, then the grid-view payload if the node's value is a griddable matrix, * then `_lazy` last, so a row is the same shape whichever builder made it. None rewrites the - * row's columns. + * row's columns. The object's own properties beyond its columns go under `_props`, before the + * flags. * * The pool shares the FINISHED row, rather than being passed to `toRow()`: the row is the * node's cells plus the host's own, so sharing here covers all of them in one walk where doing diff --git a/dist/model/rows/rowStamp.js b/dist/model/rows/rowStamp.js index 3cee8689..b313b922 100644 --- a/dist/model/rows/rowStamp.js +++ b/dist/model/rows/rowStamp.js @@ -1,5 +1,6 @@ import { stampMatrix } from '../matrix/matrixPayload.js'; import { stampLazy } from './lazyRows.js'; +import { propertyCells } from './propertyCells.js'; /** The mark key for one entry. `\0` cannot occur in a section or entry name. */ export function clipMarkKey(section, name) { return `${section}\u0000${name}`; @@ -80,7 +81,8 @@ export function sharedIdTest() { * In this order, which both builders already applied: the entry's Status and clip mark, then * the capability flags, then the grid-view payload if the node's value is a griddable matrix, * then `_lazy` last, so a row is the same shape whichever builder made it. None rewrites the - * row's columns. + * row's columns. The object's own properties beyond its columns go under `_props`, before the + * flags. * * The pool shares the FINISHED row, rather than being passed to `toRow()`: the row is the * node's cells plus the host's own, so sharing here covers all of them in one walk where doing @@ -102,6 +104,11 @@ export function stampRow(built, n, ctx) { const flags = ctx.sharedId ? { _canCopy: false, _canDelete: false, _canAddChild: false } : capabilityFlags(n, { canCopy: ctx.canCopy }); + // The object's own properties the row has no column for, which a property column the user + // names reads (propertyCells.ts). Only on a row whose object has any. + const props = propertyCells(n, row); + if (props) + row = { ...row, _props: props }; const stamped = stampLazy(stampMatrix({ ...row, ...flags }, n), ctx.deferred); return ctx.pool ? ctx.pool.share(stamped) : stamped; } diff --git a/dist/model/rows/tableRow.d.ts b/dist/model/rows/tableRow.d.ts index c07865ca..5d3ef00b 100644 --- a/dist/model/rows/tableRow.d.ts +++ b/dist/model/rows/tableRow.d.ts @@ -61,6 +61,12 @@ export interface TreeTableRow { _systemPath?: string; _blockPath?: string; _entryCount?: number; + /** + * The object's own properties the row has no column for, each its value as text, by the + * property's name (`{ SampleTime: '-1', MyPropA: '5' }`): what a property column the user adds + * in the Columns menu shows. Absent on a row whose object has none (propertyCells.ts). + */ + _props?: Record; /** * Every other cell the row has, by column key: a dictionary entry's `Min`, `Max`, `Unit`, …, * a block row's parameter links. Each column's cell has a shape of its own, which the table diff --git a/dist/widgets/tree-table/dex-tree-table.d.ts b/dist/widgets/tree-table/dex-tree-table.d.ts index aa76761b..55f1fcc7 100644 --- a/dist/widgets/tree-table/dex-tree-table.d.ts +++ b/dist/widgets/tree-table/dex-tree-table.d.ts @@ -112,7 +112,8 @@ export interface TableScrolledDetail { } /** * The tree table: a document's rows as a tree, with a search bar, sortable, resizable and - * reorderable columns (the Columns menu shows, hides and orders them, and a right-click on a + * reorderable columns (the Columns menu shows, hides and orders them, each within its group, and + * adds a column for any property the user names, from the rows' `_props`; a right-click on a * header filters on that column), cell editors, row drag and drop, links, matrix glyphs, an * `+ Add` gallery and virtual scrolling for very large documents. Data comes in by properties * (`rows` above all) and every user action goes out as a `dex-*` event; it never changes the @@ -148,8 +149,9 @@ export declare class DexTreeTable extends LitElement { */ columnLabels: Record | null; /** - * The group heading each column sits under in the Columns menu, by column key. Default null: - * no headings. Property only. + * The group heading each column sits under in the Columns menu, by column key. The columns are + * shown group by group, and the user moves a column within its group only, in the menu and by + * its header. Default null: no headings. Property only. */ columnGroups: Record | null; /** @@ -364,9 +366,12 @@ export declare class DexTreeTable extends LitElement { private _columnMenuOpen; private _columnMenuX; private _columnMenuY; + private _columnMenuMaxHeight; private _menuDragCol; private _menuDragOverCol; private _menuDragOverSide; + private _propertyColumns; + private _propertyNotice; private _addGalleryOpen; private _addPinned; private _filterPopupCol; @@ -401,8 +406,14 @@ export declare class DexTreeTable extends LitElement { private _readKnownColumns; private _persistOrder; private _persistHidden; + private _persistPropertyColumns; private get _availableColumns(); + private _groupOf; + private _sameGroup; + private get _orderedBuiltins(); private get _orderedColumns(); + private _columnLabel; + private get _columnVocabulary(); private get _visibleColumns(); private _getColWidth; private _defaultColWidth; @@ -413,6 +424,7 @@ export declare class DexTreeTable extends LitElement { private _onHeaderDragOver; private _onHeaderDragLeave; private _onHeaderDrop; + private _moveColumn; private _onHeaderDragEnd; private _onAddButtonClick; /** @@ -443,6 +455,11 @@ export declare class DexTreeTable extends LitElement { private _onColumnFilterCleared; private _toggleColumnVisibility; private _resetColumns; + private _addPropertyColumn; + private _removePropertyColumn; + private _propertyColumnsChanged; + private _onPropertyInputKeyDown; + private _addTypedPropertyColumn; private _onMenuDragStart; private _onMenuDragOver; private _onMenuDragLeave; @@ -617,6 +634,7 @@ export declare class DexTreeTable extends LitElement { private _renderColumnsButton; private _renderColumnFilterPopup; private _renderColumnMenu; + private _renderColumnMenuItem; } /** * Registers the widget as ``, or under `tag`, after the parts its template draws, diff --git a/dist/widgets/tree-table/dex-tree-table.js b/dist/widgets/tree-table/dex-tree-table.js index 32a0c072..2e6e65a2 100644 --- a/dist/widgets/tree-table/dex-tree-table.js +++ b/dist/widgets/tree-table/dex-tree-table.js @@ -133,6 +133,33 @@ const DEFAULT_HIDDEN_COLUMNS = ['Kind', 'Class', 'dimensions', 'dimensionsMode', // See _loadPersistedState. Kept beside the order and the hidden set, under the table's // storageKey: `dex-known-columns` by default. const KNOWN_COLUMNS_KEY = 'known-columns'; +// A property column: one the user adds by naming a property in the Columns menu's Other +// Properties group. Its key is this prefix and the property's name, so it is never taken for a +// built-in column whatever columns a document has (a dictionary's Value, a project's Type), and +// its cells are the rows' `_props` of that name (the model's propertyCells), read-only and empty +// on a row whose object has no such property. Kept, in order, under the table's storageKey: +// `dex-property-columns` by default; whether each is shown is in the hidden set, as for any column. +const PROPERTY_COLUMN_PREFIX = 'prop:'; +const PROPERTY_COLUMNS_KEY = 'property-columns'; +// The Columns menu's last group, which holds the property columns. Its id is no group label a +// host could give a built-in column (columnGroups). +const PROPERTY_GROUP = '\u0000properties'; +const PROPERTY_GROUP_LABEL = 'Other Properties'; +// A name a property column may have: a MATLAB property name (isvarname: a letter, then letters, +// digits and underscores, 63 at most). +const PROPERTY_NAME = /^[A-Za-z][A-Za-z0-9_]{0,62}$/; +// The Columns menu's least height, and the gap it keeps above the table's bottom edge. +const COLUMN_MENU_MIN_HEIGHT = 120; +const COLUMN_MENU_BOTTOM_MARGIN = 4; +function isPropertyColumn(col) { + return col.startsWith(PROPERTY_COLUMN_PREFIX); +} +function propertyColumnKey(name) { + return PROPERTY_COLUMN_PREFIX + name; +} +function propertyNameOf(col) { + return col.slice(PROPERTY_COLUMN_PREFIX.length); +} // The columns where an empty read-only cell means "this property does not exist for this // kind of object" rather than "this property is unset". Only these get the not-applicable // wash; see _cellStateClass for the measurement that picked them, and issue #26. @@ -242,7 +269,8 @@ const OP_WORD_TEXT = { }; /** * The tree table: a document's rows as a tree, with a search bar, sortable, resizable and - * reorderable columns (the Columns menu shows, hides and orders them, and a right-click on a + * reorderable columns (the Columns menu shows, hides and orders them, each within its group, and + * adds a column for any property the user names, from the rows' `_props`; a right-click on a * header filters on that column), cell editors, row drag and drop, links, matrix glyphs, an * `+ Add` gallery and virtual scrolling for very large documents. Data comes in by properties * (`rows` above all) and every user action goes out as a `dex-*` event; it never changes the @@ -279,8 +307,9 @@ export class DexTreeTable extends LitElement { */ this.columnLabels = null; /** - * The group heading each column sits under in the Columns menu, by column key. Default null: - * no headings. Property only. + * The group heading each column sits under in the Columns menu, by column key. The columns are + * shown group by group, and the user moves a column within its group only, in the menu and by + * its header. Default null: no headings. Property only. */ this.columnGroups = null; /** @@ -483,9 +512,15 @@ export class DexTreeTable extends LitElement { this._columnMenuOpen = false; this._columnMenuX = 0; this._columnMenuY = 0; + this._columnMenuMaxHeight = 0; this._menuDragCol = null; this._menuDragOverCol = null; this._menuDragOverSide = null; + // The property columns the user added, by property name, in their order (the Other Properties + // group's, and so the table's after every built-in column). + this._propertyColumns = []; + // Why the name typed into Other Properties was not added; '' when there is nothing to say. + this._propertyNotice = ''; // The Add gallery: whether it shows, and nothing else. Unlike the column menu, none of its // geometry is here — it is anchored under the search bar in the stylesheet (see the // dex-add-gallery rule), which is why a resize of the editor tab needs no notification to @@ -1017,15 +1052,135 @@ export class DexTreeTable extends LitElement { outline: 1px solid var(--dex-color-accent, #0078d4); } + /* Three parts, as the in-MATLAB data explorer's Columns dropdown: the title and Reset at + the top and Other Properties at the bottom keep their size, and the built-in columns + between them are what scrolls. The menu scrolls as a whole only when even the parts' + least heights do not fit its max-height (set on open from the table's bottom). */ .column-menu { position: fixed; z-index: 1000; + display: flex; + flex-direction: column; + box-sizing: border-box; + overflow-y: auto; background: var(--dex-bg-primary, #fff); border: 1px solid var(--dex-border-color, #d0d0d0); border-radius: 4px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15); - padding: 4px 0; - min-width: 200px; + width: 260px; + font-size: 12px; + } + + .column-menu-top { + flex: 0 0 auto; + display: flex; + align-items: center; + gap: 8px; + padding: 6px 8px 4px 10px; + border-bottom: 1px solid var(--dex-border-color-light, #e0e0e0); + } + + .column-menu-title { + flex: 1 1 auto; + font-weight: 600; + white-space: nowrap; + } + + .column-menu-builtins { + flex: 1 1 auto; + min-height: 60px; + overflow-y: auto; + padding: 2px 0 4px; + } + + .column-menu-bottom { + flex: 0 0 auto; + display: flex; + flex-direction: column; + border-top: 1px solid var(--dex-border-color-light, #e0e0e0); + padding-bottom: 6px; + } + + /* Four rows, then a scroll of its own, so the field below stays in view. */ + .column-menu-customs { + max-height: 104px; + overflow-y: auto; + } + + .column-menu-add { + display: flex; + gap: 4px; + padding: 4px 8px 0; + } + + .column-menu-input { + flex: 1 1 auto; + min-width: 0; + box-sizing: border-box; + height: 22px; + padding: 0 6px; + border: 1px solid var(--dex-border-color, #d0d0d0); + border-radius: 3px; + background: var(--dex-bg-primary, #fff); + color: inherit; + font: inherit; + } + + .column-menu-input:focus { + outline: none; + border-color: var(--dex-color-accent, #0078d4); + } + + /* The notice below says why the typed name was not added. */ + .column-menu-input[aria-invalid='true'] { + border-color: var(--dex-color-error, #d32f2f); + } + + .column-menu-add-button, + .column-menu-reset { + flex: 0 0 auto; + box-sizing: border-box; + height: 22px; + padding: 0 8px; + border: 1px solid var(--dex-border-color, #d0d0d0); + border-radius: 3px; + background: var(--dex-bg-primary, #fff); + color: inherit; + font: inherit; + white-space: nowrap; + cursor: pointer; + } + + .column-menu-add-button:hover, + .column-menu-reset:hover { + background: var(--dex-bg-hover, #e8e8e8); + } + + .column-menu-add-button:focus-visible, + .column-menu-reset:focus-visible { + outline: 1px solid var(--dex-color-accent, #0078d4); + } + + .column-menu-notice { + padding: 4px 8px 0; + color: var(--dex-color-error, #d32f2f); + white-space: normal; + } + + .column-menu-remove { + flex: 0 0 auto; + padding: 0 4px; + border: none; + background: none; + color: var(--dex-color-text-muted, #666); + font: inherit; + line-height: 1; + cursor: pointer; + } + + .column-menu-remove:hover, + .column-menu-remove:focus-visible { + color: var(--dex-color-text, #333); } .column-menu-item { @@ -1074,12 +1229,9 @@ export class DexTreeTable extends LitElement { .column-menu-item .col-label { flex: 1 1 auto; - } - - .column-menu-separator { - height: 1px; - margin: 4px 0; - background: var(--dex-border-color-light, #e0e0e0); + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; } .column-menu-group-header { @@ -1093,25 +1245,6 @@ export class DexTreeTable extends LitElement { cursor: default; } - .column-menu-reset { - display: block; - width: calc(100% - 16px); - margin: 0 8px 2px; - padding: 4px 8px; - border: none; - border-radius: 3px; - background: transparent; - color: var(--dex-color-accent, #0078d4); - font-size: 12px; - font-family: inherit; - text-align: left; - cursor: pointer; - outline: none; - } - - .column-menu-reset:hover { - background: var(--dex-bg-hover, #e8e8e8); - } .column-menu-item.drag-over-top { box-shadow: inset 0 2px 0 0 var(--dex-color-accent, #0078d4); @@ -2180,6 +2313,18 @@ export class DexTreeTable extends LitElement { catch { /* ignore */ } + try { + const namesJson = localStorage.getItem(`${key}-${PROPERTY_COLUMNS_KEY}`); + if (namesJson) { + const arr = JSON.parse(namesJson); + if (Array.isArray(arr)) { + this._propertyColumns = [...new Set(arr.filter((n) => typeof n === 'string' && PROPERTY_NAME.test(n)))]; + } + } + } + catch { + /* ignore */ + } } // The built-in columns the saved column state was saved against, or null when there is // no record (state saved before it existed) or the record cannot be read. Its own @@ -2210,14 +2355,33 @@ export class DexTreeTable extends LitElement { localStorage.setItem(`${key}-hidden-columns`, JSON.stringify([...this._hiddenColumns])); localStorage.setItem(`${key}-${KNOWN_COLUMNS_KEY}`, JSON.stringify(DEFAULT_COLUMN_ORDER)); } + _persistPropertyColumns() { + const key = this.storageKey; + if (!key) + return; + localStorage.setItem(`${key}-${PROPERTY_COLUMNS_KEY}`, JSON.stringify(this._propertyColumns)); + } // The set of columns this document supports. When the host provides `columns` // that is authoritative; otherwise fall back to the built-in default order. get _availableColumns() { return this.columns || [...Object.keys(DEFAULT_WIDTHS)]; } - // The user's preferred order restricted to the available columns, appending any - // available column the saved order doesn't mention (e.g. a newly added column). - get _orderedColumns() { + // The group a column is in: its host group (columnGroups), null for one with none, and Other + // Properties for a property column. A column moves only within its group. + _groupOf(col) { + if (isPropertyColumn(col)) + return PROPERTY_GROUP; + return this.columnGroups?.[col] ?? null; + } + _sameGroup(a, b) { + return this._groupOf(a) === this._groupOf(b); + } + // The built-in columns: the user's preferred order restricted to the available columns, + // appending any available column the saved order doesn't mention (e.g. a newly added column), + // then put group by group, each group where the document's own order first has it. A reorder + // stays within its group, so this keeps the order a user made; it regroups an order saved + // before that rule, or one a host's new grouping interleaves, and keeps each group's own order. + get _orderedBuiltins() { const available = this._availableColumns; const availableSet = new Set(available); const ordered = this._columnOrder.filter((c) => availableSet.has(c)); @@ -2225,7 +2389,40 @@ export class DexTreeTable extends LitElement { if (!ordered.includes(c)) ordered.push(c); } - return ordered; + if (!this.columnGroups) + return ordered; + const rank = new Map(); + for (const c of available) { + const group = this._groupOf(c); + if (!rank.has(group)) + rank.set(group, rank.size); + } + return ordered + .map((col, at) => ({ col, at, rank: rank.get(this._groupOf(col)) ?? rank.size })) + .sort((a, b) => a.rank - b.rank || a.at - b.at) + .map((c) => c.col); + } + // Every column in display order: the built-in ones, then the property columns, which are the + // menu's last group and so the table's last columns. + get _orderedColumns() { + return [...this._orderedBuiltins, ...this._propertyColumns.map(propertyColumnKey)]; + } + // The header and menu text of a column: its host label, its key, or a property column's name. + _columnLabel(col) { + if (isPropertyColumn(col)) + return propertyNameOf(col); + return this.columnLabels?.[col] || col; + } + // What a search may name a column by: the host's columns and labels, and each property column + // by its name. + get _columnVocabulary() { + if (this._propertyColumns.length === 0) + return { labels: this.columnLabels, keys: this.columns }; + const keys = this._propertyColumns.map(propertyColumnKey); + const labels = { ...(this.columnLabels ?? {}) }; + for (const col of keys) + labels[col] = propertyNameOf(col); + return { labels, keys: this.columns ? [...this.columns, ...keys] : null }; } get _visibleColumns() { return this._orderedColumns.filter((c) => !this._hiddenColumns.has(c)); @@ -2381,7 +2578,9 @@ export class DexTreeTable extends LitElement { } } _onHeaderDragOver(col, e) { - if (!this._dragColId || this._dragColId === col || col === 'Name') + // Not a drop target outside the dragged column's group: the default is left alone, so the + // pointer says no drop, and no drop line is drawn there. + if (!this._dragColId || this._dragColId === col || col === 'Name' || !this._sameGroup(this._dragColId, col)) return; e.preventDefault(); if (e.dataTransfer) { @@ -2419,32 +2618,42 @@ export class DexTreeTable extends LitElement { this._dragColId = null; this._dragOverColId = null; this._dragOverSide = null; - // Splice `_orderedColumns`, NOT the raw `_columnOrder` — the same array the - // menu reorder edits. The raw order can be missing a column the user just - // dragged: it is whatever was persisted (possibly by an older version, or by - // a document with a different column set), while `_orderedColumns` is what - // the header row actually renders. Using the raw array made both indexOf - // lookups able to miss, and neither miss was benign — `fromIdx < 0` made the - // drop a silent no-op (every header reorder dead in a .prj view, whose - // columns are Type/Location/Labels), and `toIdx < 0` MISPLACED the column, - // since splice(-1, 0, x) inserts before the last element and the 'right' - // side turns -1 into 0, landing it ahead of the pinned Name. The bad order - // was then persisted, so it followed the user into every later session. - const order = [...this._orderedColumns]; + this._moveColumn(dragged, col, side === 'right'); + } + // Move `dragged` before `target`, or after it, within their group: the one reorder both the + // header and the menu make. A move into another group, or one that would take or displace + // the pinned Name, does nothing. A property column moves among the property columns; a + // built-in one among the built-in ones, which are saved as the order. + // + // Splice `_orderedBuiltins`, NOT the raw `_columnOrder`. The raw order can be missing a + // column the user just dragged: it is whatever was persisted (possibly by an older version, + // or by a document with a different column set), while `_orderedBuiltins` is what the header + // row actually renders. Using the raw array made both indexOf lookups able to miss, and + // neither miss was benign — `fromIdx < 0` made the drop a silent no-op (every header + // reorder dead in a .prj view, whose columns are Type/Location/Labels), and `toIdx < 0` + // MISPLACED the column, since splice(-1, 0, x) inserts before the last element and the + // 'right' side turns -1 into 0, landing it ahead of the pinned Name. The bad order was then + // persisted, so it followed the user into every later session. + _moveColumn(dragged, target, after) { + if (dragged === target || dragged === 'Name' || target === 'Name' || !this._sameGroup(dragged, target)) + return; + const property = isPropertyColumn(dragged); + const order = property ? this._propertyColumns.map(propertyColumnKey) : [...this._orderedBuiltins]; const fromIdx = order.indexOf(dragged); - const toIdx0 = order.indexOf(col); - // Both are present by construction (only rendered headers are draggable and + // Both are present by construction (only rendered headers and items are draggable and // droppable); kept as a guard so a future caller can't reintroduce a -1. - if (fromIdx < 0 || toIdx0 < 0) + if (fromIdx < 0 || !order.includes(target)) return; order.splice(fromIdx, 1); - let toIdx = order.indexOf(col); - if (side === 'right') { - toIdx += 1; + order.splice(order.indexOf(target) + (after ? 1 : 0), 0, dragged); + if (property) { + this._propertyColumns = order.map(propertyNameOf); + this._persistPropertyColumns(); + } + else { + this._columnOrder = order; + this._persistOrder(); } - order.splice(toIdx, 0, dragged); - this._columnOrder = order; - this._persistOrder(); } _onHeaderDragEnd(_e) { this._dragColId = null; @@ -2541,6 +2750,14 @@ export class DexTreeTable extends LitElement { // the right edge doesn't push the dropdown off-screen and truncate labels. this._columnMenuX = Math.max(0, window.innerWidth - rect.right); this._columnMenuY = rect.bottom + 2; + // No taller than the table below the button (else the window), so Reset at its top and + // Other Properties at its bottom stay on screen and the columns between them scroll. A + // floor, so a table too short for the menu does not collapse it; a host with no layout + // (all zeros) gets the window's height. + const hostBottom = this.getBoundingClientRect().bottom; + const bottom = hostBottom > this._columnMenuY ? Math.min(hostBottom, window.innerHeight || hostBottom) : window.innerHeight; + this._columnMenuMaxHeight = Math.max(COLUMN_MENU_MIN_HEIGHT, bottom - this._columnMenuY - COLUMN_MENU_BOTTOM_MARGIN); + this._propertyNotice = ''; this._columnMenuOpen = true; } // --- Per-column filter popup --- @@ -2584,7 +2801,7 @@ export class DexTreeTable extends LitElement { // `Name:a Name:b` — two conditions ANDed, so nothing matches, which reads as a // bug rather than as a replaced filter. _applyColumnFilter(col, op, value) { - const label = this.columnLabels?.[col] || col; + const label = this._columnLabel(col); const text = formatToken(label, op, value); const existing = this._tokenForColumn(col); const next = existing @@ -2631,14 +2848,101 @@ export class DexTreeTable extends LitElement { this._hiddenColumns = updated; this._persistHidden(); } - // Restore the default column order and visibility, discarding any customization. + // Restore the default order and visibility of the built-in columns, discarding any + // customization of them. The property columns stay as they are, each shown or hidden as it + // was: they are the ones the user named, and × is how one goes. _resetColumns(e) { e.stopPropagation(); this._columnOrder = [...DEFAULT_COLUMN_ORDER]; - this._hiddenColumns = new Set(DEFAULT_HIDDEN_COLUMNS); + const hidden = new Set(DEFAULT_HIDDEN_COLUMNS); + for (const col of this._hiddenColumns) + if (isPropertyColumn(col)) + hidden.add(col); + this._hiddenColumns = hidden; this._persistOrder(); this._persistHidden(); } + // --- Property columns (the menu's Other Properties) --- + // The name typed into Other Properties, added: a built-in column's key or label (in any + // case, so `storage class` or `DataType`) shows that column; a property name not in the + // list becomes a shown property column at the end; one in the list but hidden is shown. + // Anything else says why it was not added (_propertyNotice). Answers whether the name was + // taken, so the field can be emptied. + _addPropertyColumn(typed) { + const name = typed.trim(); + if (name === '') + return false; + const lower = name.toLowerCase(); + const builtIn = this._availableColumns.find((c) => c.toLowerCase() === lower || (this.columnLabels?.[c] ?? '').toLowerCase() === lower); + const col = builtIn ?? propertyColumnKey(name); + if (!builtIn) { + if (!PROPERTY_NAME.test(name)) { + this._propertyNotice = `"${name}" is not a valid property name.`; + return false; + } + if (this._propertyColumns.includes(name) && !this._hiddenColumns.has(col)) { + this._propertyNotice = `"${name}" is already in the list.`; + return false; + } + if (!this._propertyColumns.includes(name)) { + this._propertyColumns = [...this._propertyColumns, name]; + this._persistPropertyColumns(); + this._propertyColumnsChanged(); + } + } + this._propertyNotice = ''; + if (this._hiddenColumns.has(col)) + this._toggleColumnVisibility(col); + return true; + } + // A property column taken off the table, with everything that was about it: whether it was + // hidden, its width, its sort, and a filter popup open on it. + _removePropertyColumn(name) { + const col = propertyColumnKey(name); + this._propertyColumns = this._propertyColumns.filter((n) => n !== name); + if (this._hiddenColumns.has(col)) { + const hidden = new Set(this._hiddenColumns); + hidden.delete(col); + this._hiddenColumns = hidden; + this._persistHidden(); + } + if (this._columnWidths.has(col)) { + const widths = new Map(this._columnWidths); + widths.delete(col); + this._columnWidths = widths; + } + if (this._sortState.some((s) => s.column === col)) + this._sortState = this._sortState.filter((s) => s.column !== col); + if (this._filterPopupCol === col) + this._filterPopupCol = null; + this._propertyNotice = ''; + this._persistPropertyColumns(); + this._propertyColumnsChanged(); + } + // The search's vocabulary has another column, or one fewer: a qualified term may name it. + _propertyColumnsChanged() { + this._filterParseCache = null; + this._visibleRowsCache = null; + } + _onPropertyInputKeyDown(e) { + // The field's keys are the field's: none reaches the table or the menu around it. + e.stopPropagation(); + if (e.key === 'Enter') { + e.preventDefault(); + this._addTypedPropertyColumn(); + } + else if (e.key === 'Escape') { + // Closed, and the focus back on the button that opened it, not dropped on the page. + e.preventDefault(); + this._columnMenuOpen = false; + this.shadowRoot?.querySelector('.columns-button')?.focus(); + } + } + _addTypedPropertyColumn() { + const input = this.shadowRoot?.querySelector('.column-menu-input'); + if (input && this._addPropertyColumn(input.value)) + input.value = ''; + } // --- Column reordering from the menu list --- _onMenuDragStart(col, e) { if (col === 'Name') { @@ -2652,7 +2956,9 @@ export class DexTreeTable extends LitElement { } } _onMenuDragOver(col, e) { - if (!this._menuDragCol || col === 'Name') + // Only an item of the dragged column's group takes the drop: elsewhere the default is left + // alone, so the pointer says no drop and no drop line is drawn. + if (!this._menuDragCol || col === 'Name' || !this._sameGroup(this._menuDragCol, col)) return; e.preventDefault(); if (e.dataTransfer) @@ -2674,20 +2980,10 @@ export class DexTreeTable extends LitElement { this._menuDragOverCol = null; const side = this._menuDragOverSide; this._menuDragOverSide = null; - // Name is pinned first and can neither move nor be displaced. - if (!dragged || dragged === col || dragged === 'Name' || col === 'Name') + // Name is pinned first and can neither move nor be displaced; nothing leaves its group. + if (!dragged) return; - const order = [...this._orderedColumns]; - const fromIdx = order.indexOf(dragged); - if (fromIdx < 0) - return; - order.splice(fromIdx, 1); - let toIdx = order.indexOf(col); - if (side === 'bottom') - toIdx += 1; - order.splice(toIdx, 0, dragged); - this._columnOrder = order; - this._persistOrder(); + this._moveColumn(dragged, col, side === 'bottom'); } _onMenuDragEnd() { this._menuDragCol = null; @@ -2764,7 +3060,7 @@ export class DexTreeTable extends LitElement { _renderFunnel(col) { if (this.fixedRows) return nothing; - const label = this.columnLabels?.[col] || col; + const label = this._columnLabel(col); const active = this._tokenForColumn(col) !== undefined; return html ` + +
+ ${builtIns.map((col) => { + const group = this._groupOf(col); + const header = group && group !== lastGroup ? html `
${group}
` : nothing; + lastGroup = group; + return html `${header}${this._renderColumnMenuItem(col)}`; + })} +
+
+
${PROPERTY_GROUP_LABEL}
+ ${this._propertyColumns.length > 0 + ? html `
+ ${this._propertyColumns.map((name) => this._renderColumnMenuItem(propertyColumnKey(name)))} +
` + : nothing} +
+ this._onPropertyInputKeyDown(e)} + @input=${() => (this._propertyNotice = '')} + /> + +
+ ${this._propertyNotice + ? html `
${this._propertyNotice}
` + : nothing} +
+ + `; + } + // One column of the menu: its grip, its box and its name, and a property column's ×. The whole + // row is draggable, within its group. + _renderColumnMenuItem(col) { + const isName = col === 'Name'; + const property = isPropertyColumn(col); + const isVisible = !this._hiddenColumns.has(col); + const overCls = this._menuDragOverCol === col ? (this._menuDragOverSide === 'top' ? 'drag-over-top' : 'drag-over-bottom') : ''; + const draggingCls = this._menuDragCol === col ? 'dragging' : ''; + const label = this._columnLabel(col); + return html ` +