Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ release notes before you move the pin.

```json
"dependencies": {
"data-explorer-view": "github:mathworks/data-explorer-view#v1.10.1",
"data-explorer-view": "github:mathworks/data-explorer-view#v1.11.0",
"data-explorer-core": "github:mathworks/data-explorer-core#v1.59.0",
"lit": "^3.2.0"
}
Expand Down
9 changes: 6 additions & 3 deletions dist/model/edit/binaryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { sourceWarnings, warningBanner } from '../rows/parseWarnings.js';
import { buildEntryRows, COLUMNS, COLUMN_GROUPS, COLUMN_LABELS } from '../rows/rowBuilder.js';
import { rowPlannerFor } from '../rows/rowPlanner.js';
import { SourceClosed, } from '../source/tableSource.js';
import { copyEntriesToClipboard, dragEntries } from './clipboardAction.js';
import { copyEntriesToClipboard, dragEntries, entriesUnder, readTaken } from './clipboardAction.js';
import { clipMarkChange, clipMarkOfDoc } from './clipMarks.js';
import { sectionTarget } from './dictionaryTarget.js';
import { answerEditTextRequest } from './editText.js';
Expand Down Expand Up @@ -84,6 +84,8 @@ export class BinaryView {
drag: (descriptor) => this.emit({ kind: 'drag', descriptor }),
});
registers.registerSourceDeleter(doc.uri, (targets) => this.deleteMoved(targets));
// And so that a paste of what a Copy or a Cut took from it reads the entries as they are now.
registers.registerSourceReader(doc.uri, (items) => readTaken(items, { session: this.session, entries: () => entriesUnder(this.doc.liveModel()), describe: (m) => this.doc.describe(m) }));
doc.views.add(this);
}
get session() {
Expand Down Expand Up @@ -817,9 +819,10 @@ export class BinaryView {
registers.clearDrag();
registers.broadcastDragState();
}
// Only once the LAST view of the document closes: both are the document's.
// Only once the LAST view of the document closes: both are the document's. A clipboard taken
// from it goes too (closeSource): there is nothing left to read it back from.
if (this.doc.views.size === 0) {
registers.unregisterSourceDeleter(this.doc.uri);
registers.closeSource(this.doc.uri);
this.doc.baseline.clear();
}
}
Expand Down
37 changes: 35 additions & 2 deletions dist/model/edit/clipboardAction.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Session } from 'data-explorer-core';
import type { ClipboardMode } from '../source/wire.js';
import type { ClipboardItem, DragRegisterItem, Registers } from './registers.js';
import type { ClipboardItem, DragRegisterItem, Registers, SourceRead } from './registers.js';
/** The part of core's session a snapshot asks. */
export type SnapshotSession = Pick<Session, 'copyRefusal' | 'findNodeById' | 'getDataSource'>;
export interface CopyDeps {
Expand Down Expand Up @@ -49,6 +49,11 @@ export declare class CopyRefused extends Error {
* document. A detached entry, or one whose section has no name, is '' as it always was.
*/
export declare function sourceSectionOf(session: Pick<Session, 'getDataSource'>, entry: any): string;
/**
* The entries of the document under `root`: the first nodes down each branch that are one (a
* MAT-file's children, a dictionary's sections' children). Nothing below an entry is one.
*/
export declare function entriesUnder(root: any): any[];
/**
* Snapshot the rows a drag started on into the drag-register shape: the same walk, dedupe and
* refusal as a copy, so "if you can cut/copy you can drag". Each item keeps its OWN source
Expand All @@ -60,14 +65,42 @@ export declare function buildDragSnapshot(session: SnapshotSession, rowIds: unkn
};
/**
* Snapshot the rows a copy/cut acts on into the clipboard shape: the same walk and the same
* dedupe as buildDragSnapshot. Each item keeps its OWN source section: a cut is lazy, so the
* dedupe as buildDragSnapshot. What a paste lands is this snapshot read back from its document at
* paste time (readTaken), by who each entry was. Each item keeps its OWN source section: a cut is lazy, so the
* source deletion at paste time must find each entry where it actually lives.
*/
export declare function buildClipboardSnapshot(session: SnapshotSession, rowIds: unknown, findNode: (rowId: string) => any, originOf?: (entry: any) => object | undefined): ClipboardItem[];
/** What a document's reader needs of it to read back what a Copy or a Cut took (readTaken). */
export interface ReadDeps {
/** Core's session, which the entries are asked of (copyRefusal) and resolved in. */
session: SnapshotSession;
/**
* The document's entries as they are now, its model first brought up to date with its live
* content (a JSON dictionary re-parses its text): once per paste, however many entries it reads.
*/
entries: () => Iterable<any>;
/** The token its document tells an entry by (CopyDeps.originOf), where its selectors cannot. */
originOf?: (entry: any) => object | undefined;
/** Core's message in this document's words (a srcId said as the file's name). Default: as it is. */
describe?: (message: string) => string;
}
/**
* The entries `held` took from this document, as they are now (registers.ts's SourceReader): each
* found again by who it was, then taken again as a copy takes it, so the paste lands what the user
* sees and a cut's delete names the entry as it is now. Who an entry was is its origin token where
* it was taken with one (a MAT-file's variable: through edits and a rename, the same node), else
* its uuid (a dictionary entry's, which a rename keeps), else its name in the section it was taken
* from. One found nowhere, deleted since, is named in `gone`. A refusal (an entry that can no
* longer travel, a model that no longer reads) throws TransferRefused, in this document's words.
*/
export declare function readTaken(held: readonly ClipboardItem[], deps: ReadDeps): SourceRead;
/**
* Snapshot every entry the rows resolve to onto the clipboard in `mode`.
*
* Rows are deduped by owning entry, so selecting a bus and two of its elements copies ONE bus.
* Copy and Cut alike are read back from their document when they are pasted (readTaken): the
* snapshot says which entries, and the paste lands them as they are then.
*
* `uriString` is recorded with the payloads because a cut is LAZY — the source delete is deferred
* to paste time, and the paste may land in a different document, so the clipboard must remember
* which document to delete from.
Expand Down
85 changes: 77 additions & 8 deletions dist/model/edit/clipboardAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Everything format-specific is injected: `refresh` (bring the model up to date with the live
// content) and `findNode` (resolve a row id against it) are the ONLY real difference between the
// formats here, never what copying a row means.
import { entrySelectorOf, owningEntryOf } from 'data-explorer-core';
import { TransferRefused, entrySelectorOf, owningEntryOf } from 'data-explorer-core';
import { dropFactsOf } from '../actions/dropFacts.js';
import { WHOLE_FILE_SECTION } from '../ids/sectionRowId.js';
import { sharedNameRefusal } from './transfer.js';
Expand Down Expand Up @@ -116,21 +116,29 @@ function entriesSharingAName(entries) {
}
return shared;
}
// How many entries of the document under `root` answer to each name: its entries are the
// first nodes down each branch that are one (a MAT-file's children, a dictionary's sections'
// children). Nothing below an entry is one.
// How many entries of the document under `root` answer to each name.
function entryNameCounts(root) {
const counts = new Map();
for (const entry of entriesUnder(root))
counts.set(entry.name, (counts.get(entry.name) ?? 0) + 1);
return counts;
}
/**
* The entries of the document under `root`: the first nodes down each branch that are one (a
* MAT-file's children, a dictionary's sections' children). Nothing below an entry is one.
*/
export function entriesUnder(root) {
const entries = [];
const visit = (node) => {
for (const child of (node?.children ?? [])) {
if (child?.isEntry)
counts.set(child.name, (counts.get(child.name) ?? 0) + 1);
entries.push(child);
else
visit(child);
}
};
visit(root);
return counts;
return entries;
}
/**
* Each row's owning entry as a register item: what to paste, the section it came from, the
Expand All @@ -139,7 +147,10 @@ function entryNameCounts(root) {
* by. The one build behind both registers, after the one refusal they share.
*/
function registerItemsOf(session, rowIds, findNode, originOf) {
const entries = owningEntriesOf(rowIds, findNode);
return itemsOfEntries(session, owningEntriesOf(rowIds, findNode), originOf);
}
// The register items of `entries`, as a copy, a cut and a drag take them, after the refusal they share.
function itemsOfEntries(session, entries, originOf) {
refuseUncopyable(session, entries);
const shared = entriesSharingAName(entries);
return entries.map((entry) => {
Expand All @@ -164,16 +175,74 @@ export function buildDragSnapshot(session, rowIds, findNode, originOf) {
}
/**
* Snapshot the rows a copy/cut acts on into the clipboard shape: the same walk and the same
* dedupe as buildDragSnapshot. Each item keeps its OWN source section: a cut is lazy, so the
* dedupe as buildDragSnapshot. What a paste lands is this snapshot read back from its document at
* paste time (readTaken), by who each entry was. Each item keeps its OWN source section: a cut is lazy, so the
* source deletion at paste time must find each entry where it actually lives.
*/
export function buildClipboardSnapshot(session, rowIds, findNode, originOf) {
return registerItemsOf(session, rowIds, findNode, originOf);
}
/**
* The entries `held` took from this document, as they are now (registers.ts's SourceReader): each
* found again by who it was, then taken again as a copy takes it, so the paste lands what the user
* sees and a cut's delete names the entry as it is now. Who an entry was is its origin token where
* it was taken with one (a MAT-file's variable: through edits and a rename, the same node), else
* its uuid (a dictionary entry's, which a rename keeps), else its name in the section it was taken
* from. One found nowhere, deleted since, is named in `gone`. A refusal (an entry that can no
* longer travel, a model that no longer reads) throws TransferRefused, in this document's words.
*/
export function readTaken(held, deps) {
const said = (message) => (deps.describe ? deps.describe(message) : message);
let live;
try {
live = [...deps.entries()];
}
catch (err) {
throw new TransferRefused(said(`Could not read the copied entries again: ${err?.message || String(err)}`));
}
const byOrigin = new Map();
const byUuid = new Map();
for (const entry of live) {
const origin = deps.originOf?.(entry);
if (origin)
byOrigin.set(origin, entry);
const { uuid } = entrySelectorOf(entry);
if (uuid)
byUuid.set(uuid, [...(byUuid.get(uuid) ?? []), entry]);
}
const sameAsTaken = (it, entry) => entry.name === it.payload.name && sourceSectionOf(deps.session, entry) === it.sourceSection;
const found = [];
const gone = [];
for (const it of held) {
const { uuid } = entrySelectorOf(it.payload);
let entry;
if (it.origin)
entry = byOrigin.get(it.origin);
else if (uuid) {
const withUuid = byUuid.get(uuid) ?? [];
entry = withUuid.length === 1 ? withUuid[0] : withUuid.find((e) => sameAsTaken(it, e));
}
else
entry = live.find((e) => sameAsTaken(it, e));
if (!entry)
gone.push(String(it.payload.name ?? ''));
else if (!found.includes(entry))
found.push(entry);
}
try {
return { items: itemsOfEntries(deps.session, found, deps.originOf), gone };
}
catch (err) {
throw new TransferRefused(said(err instanceof CopyRefused ? err.message : `Failed to paste: ${err?.message || String(err)}`));
}
}
/**
* Snapshot every entry the rows resolve to onto the clipboard in `mode`.
*
* Rows are deduped by owning entry, so selecting a bus and two of its elements copies ONE bus.
* Copy and Cut alike are read back from their document when they are pasted (readTaken): the
* snapshot says which entries, and the paste lands them as they are then.
*
* `uriString` is recorded with the payloads because a cut is LAZY — the source delete is deferred
* to paste time, and the paste may land in a different document, so the clipboard must remember
* which document to delete from.
Expand Down
13 changes: 9 additions & 4 deletions dist/model/edit/jsonView.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { buildEntryRows, COLUMNS, COLUMN_GROUPS, COLUMN_LABELS } from '../rows/r
import { rowPlannerFor } from '../rows/rowPlanner.js';
import { SourceClosed, } from '../source/tableSource.js';
import { parseDetached } from '../workspace/SlddModel.js';
import { copyEntriesToClipboard, dragEntries } from './clipboardAction.js';
import { copyEntriesToClipboard, dragEntries, entriesUnder, readTaken } from './clipboardAction.js';
import { clipMarkChange, clipMarkOfDoc } from './clipMarks.js';
import { sectionTarget } from './dictionaryTarget.js';
import { answerEditTextRequest } from './editText.js';
Expand Down Expand Up @@ -132,6 +132,9 @@ export class JsonView {
drag: (descriptor) => this.emit({ kind: 'drag', descriptor }),
}));
registers.registerSourceDeleter(this.uri, (targets) => doc.writes.enqueue(() => this.deleteFromSourceDocument(targets)));
// And so that a paste of what a Copy or a Cut took from it reads the entries from the text as
// it stands then, whichever writer last changed it.
registers.registerSourceReader(this.uri, (items) => readTaken(items, { session: this.session, entries: () => entriesUnder(this.liveModel()), describe: (m) => this.doc.describe(m) }));
// Every tab of this document repaints wide when the baseline moves (repaintAll).
this.docState.repaints.add(this.repaintThis);
// Repaint on ANY change to the text: this table's edits, a text view's, undo and redo.
Expand Down Expand Up @@ -982,7 +985,8 @@ export class JsonView {
copyEntriesToClipboard(rowIds, 'copy', this.uri, this.copyDeps(() => this.doc.options.registers.broadcastClipboardState()));
}
// Cut is LAZY: it only marks the entry on the clipboard and writes nothing. The source is removed
// at PASTE time, so a same-document move is a single write (one undo step).
// at PASTE time, so a same-document move is a single write (one undo step), and what moves is
// the entry as the text holds it then (the registered reader).
cut(rowIds) {
if (this.disposed)
return;
Expand Down Expand Up @@ -1554,9 +1558,10 @@ export class JsonView {
done?.(true);
// Per table, paired with the open: the last one drops the tree it held.
this.doc.registry.releaseSourceView(this.uri);
// Only once the LAST table of this document closes.
// Only once the LAST table of this document closes, and a clipboard taken from it with it
// (closeSource): there is nothing left to read it back from.
if (this.doc.closeView(this.docState)) {
registers.unregisterSourceDeleter(this.uri);
registers.closeSource(this.uri);
this.doc.baseline.clear();
}
}
Expand Down
14 changes: 12 additions & 2 deletions dist/model/edit/matEngine.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,18 @@ export declare class MatEngine {
* the answer.
*/
private deleteMoved;
/**
* The variables a Copy or a Cut took from this file, as they are now, for a paste of them (the
* registers' source reader): each found by the token it was taken with (`originOf`), among the
* nodes `deleteMoved` would take it from, so the paste and a cut's delete find the same one.
*/
private readTaken;
/**
* The token a cut or a drag of `variable` carries (TransferItem.origin), which `deleteMoved`
* tells the original by: one object for as long as the variable is the same node — through
* edits of it, a rename, and the undo of its delete, which puts the node itself back. A variable
* made since (an Add, a paste, a reload of the file) is another node, with another token.
* edits of it, a rename, and the undo of its delete, which puts the node itself back — and,
* through a reload of the file, for the variable the file still holds under its name
* (`carryTokens`). A variable made since (an Add, a paste) is another node, with another token.
*/
private originOf;
private readonly origins;
Expand All @@ -226,6 +233,9 @@ export declare class MatEngine {
* every view is painted from it.
*/
replace(bytes: ArrayBuffer): Promise<void>;
private uniquelyNamed;
private tokensByName;
private carryTokens;
/** A table of the document: an EditableTableSource, until its `dispose()`. */
view(): EditableTableSource & {
dispose(): void;
Expand Down
Loading
Loading