Skip to content

Commit 9c1f7d7

Browse files
committed
chore: put the clipboard methods into a sane order
1 parent 8af1489 commit 9c1f7d7

File tree

1 file changed

+75
-70
lines changed

1 file changed

+75
-70
lines changed

src/actions/clipboard.ts

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -145,76 +145,6 @@ export class Clipboard {
145145
return 'disabled';
146146
}
147147

148-
/**
149-
* Precondition function for the copy context menu. This wraps the core copy
150-
* precondition to support context menus.
151-
*
152-
* @param scope scope of the shortcut or context menu item
153-
* @returns 'enabled' if the node can be copied, 'disabled' otherwise.
154-
*/
155-
private copyPrecondition(scope: ContextMenuRegistry.Scope): string {
156-
const focused = scope.focusedNode;
157-
if (!focused || !isCopyable(focused)) return 'hidden';
158-
159-
const workspace = focused.workspace;
160-
if (!(workspace instanceof WorkspaceSvg)) return 'hidden';
161-
162-
if (
163-
this.oldCopyShortcut?.preconditionFn &&
164-
this.oldCopyShortcut.preconditionFn(workspace, scope)
165-
) {
166-
return 'enabled';
167-
}
168-
return 'disabled';
169-
}
170-
171-
private getPasteWorkspace(
172-
scope: ContextMenuRegistry.Scope,
173-
): WorkspaceSvg | undefined {
174-
let workspace;
175-
if (scope.focusedNode instanceof WorkspaceSvg) {
176-
workspace = scope.focusedNode;
177-
} else if (isSelectable(scope.focusedNode)) {
178-
workspace = scope.focusedNode.workspace;
179-
}
180-
181-
if (!workspace || !(workspace instanceof WorkspaceSvg)) return undefined;
182-
return workspace;
183-
}
184-
185-
/**
186-
* Precondition function for the paste context menu. This wraps the core
187-
* paste precondition to support context menus.
188-
*
189-
* @param scope scope of the shortcut or context menu item
190-
* @returns 'enabled' if the node can be pasted, 'disabled' otherwise.
191-
*/
192-
private pastePrecondition(scope: ContextMenuRegistry.Scope): string {
193-
const workspace = this.getPasteWorkspace(scope);
194-
// If we can't identify what workspace to paste into, hide.
195-
if (!workspace) return 'hidden';
196-
197-
// Don't paste into flyouts.
198-
if (workspace.isFlyout) return 'hidden';
199-
200-
if (!this.options.allowCrossWorkspacePaste) {
201-
// Only paste into the same workspace that was copied from
202-
// or the parent workspace of a flyout that was copied from.
203-
let copiedWorkspace = clipboard.getLastCopiedWorkspace();
204-
if (copiedWorkspace?.isFlyout)
205-
copiedWorkspace = copiedWorkspace.targetWorkspace;
206-
if (copiedWorkspace !== workspace) return 'disabled';
207-
}
208-
209-
if (
210-
this.oldPasteShortcut?.preconditionFn &&
211-
this.oldPasteShortcut.preconditionFn(workspace, scope)
212-
) {
213-
return 'enabled';
214-
}
215-
return 'disabled';
216-
}
217-
218148
/**
219149
* The callback for the cut action. Uses the registered version of the cut callback
220150
* to perform the cut logic, then pops a toast if cut happened.
@@ -290,6 +220,29 @@ export class Clipboard {
290220
ContextMenuRegistry.registry.register(copyAction);
291221
}
292222

223+
/**
224+
* Precondition function for the copy context menu. This wraps the core copy
225+
* precondition to support context menus.
226+
*
227+
* @param scope scope of the shortcut or context menu item
228+
* @returns 'enabled' if the node can be copied, 'disabled' otherwise.
229+
*/
230+
private copyPrecondition(scope: ContextMenuRegistry.Scope): string {
231+
const focused = scope.focusedNode;
232+
if (!focused || !isCopyable(focused)) return 'hidden';
233+
234+
const workspace = focused.workspace;
235+
if (!(workspace instanceof WorkspaceSvg)) return 'hidden';
236+
237+
if (
238+
this.oldCopyShortcut?.preconditionFn &&
239+
this.oldCopyShortcut.preconditionFn(workspace, scope)
240+
) {
241+
return 'enabled';
242+
}
243+
return 'disabled';
244+
}
245+
293246
/**
294247
* The callback for the copy action. Uses the registered version of the copy callback
295248
* to perform the copy logic, then pops a toast if copy happened.
@@ -363,6 +316,58 @@ export class Clipboard {
363316
ContextMenuRegistry.registry.register(pasteAction);
364317
}
365318

319+
/**
320+
* Get the workspace to paste into based on which type of thing the menu was opened on.
321+
*
322+
* @returns WorkspaceSvg to paste into or undefined
323+
*/
324+
private getPasteWorkspace(
325+
scope: ContextMenuRegistry.Scope,
326+
): WorkspaceSvg | undefined {
327+
let workspace;
328+
if (scope.focusedNode instanceof WorkspaceSvg) {
329+
workspace = scope.focusedNode;
330+
} else if (isSelectable(scope.focusedNode)) {
331+
workspace = scope.focusedNode.workspace;
332+
}
333+
334+
if (!workspace || !(workspace instanceof WorkspaceSvg)) return undefined;
335+
return workspace;
336+
}
337+
338+
/**
339+
* Precondition function for the paste context menu. This wraps the core
340+
* paste precondition to support context menus.
341+
*
342+
* @param scope scope of the shortcut or context menu item
343+
* @returns 'enabled' if the node can be pasted, 'disabled' otherwise.
344+
*/
345+
private pastePrecondition(scope: ContextMenuRegistry.Scope): string {
346+
const workspace = this.getPasteWorkspace(scope);
347+
// If we can't identify what workspace to paste into, hide.
348+
if (!workspace) return 'hidden';
349+
350+
// Don't paste into flyouts.
351+
if (workspace.isFlyout) return 'hidden';
352+
353+
if (!this.options.allowCrossWorkspacePaste) {
354+
// Only paste into the same workspace that was copied from
355+
// or the parent workspace of a flyout that was copied from.
356+
let copiedWorkspace = clipboard.getLastCopiedWorkspace();
357+
if (copiedWorkspace?.isFlyout)
358+
copiedWorkspace = copiedWorkspace.targetWorkspace;
359+
if (copiedWorkspace !== workspace) return 'disabled';
360+
}
361+
362+
if (
363+
this.oldPasteShortcut?.preconditionFn &&
364+
this.oldPasteShortcut.preconditionFn(workspace, scope)
365+
) {
366+
return 'enabled';
367+
}
368+
return 'disabled';
369+
}
370+
366371
/**
367372
* The callback for the paste action. Uses the registered version of the paste callback
368373
* to perform the paste logic, then clears any toasts about pasting.

0 commit comments

Comments
 (0)