Skip to content

Commit 3540fd4

Browse files
committed
fix: v12 update and refactor common method
1 parent 2089f86 commit 3540fd4

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

src/actions/clipboard.ts

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class Clipboard {
105105
callback: this.cutCallback.bind(this),
106106
// The registry gives back keycodes as an object instead of an array
107107
// See https://github.com/google/blockly/issues/9008
108-
keyCodes: Object.values(oldCutShortcut.keyCodes ?? []),
108+
keyCodes: oldCutShortcut.keyCodes,
109109
allowCollision: false,
110110
};
111111

@@ -122,25 +122,12 @@ export class Clipboard {
122122
*/
123123
private registerCutContextMenuAction() {
124124
const cutAction: ContextMenuRegistry.RegistryItem = {
125-
displayText: (scope) => Msg['CUT_SHORTCUT'].replace('%1', getShortActionShortcut(Constants.SHORTCUT_NAMES.CUT)),
126-
preconditionFn: (scope) => {
127-
const focused = scope.focusedNode;
128-
129-
if (!focused || !isCopyable(focused)) return 'hidden';
130-
131-
const workspace = focused.workspace;
132-
if (
133-
!workspace.isReadOnly() &&
134-
isDeletable(focused) &&
135-
focused.isDeletable() &&
136-
isDraggable(focused) &&
137-
focused.isMovable() &&
138-
!focused.workspace.isFlyout
139-
)
140-
return 'enabled';
141-
142-
return 'disabled';
143-
},
125+
displayText: (scope) =>
126+
Msg['CUT_SHORTCUT'].replace(
127+
'%1',
128+
getShortActionShortcut(Constants.SHORTCUT_NAMES.CUT),
129+
),
130+
preconditionFn: (scope) => this.cutCopyPrecondition(scope),
144131
callback: (scope, menuOpenEvent) => {
145132
if (!isCopyable(scope.focusedNode)) return false;
146133
const ws = scope.focusedNode.workspace;
@@ -155,6 +142,34 @@ export class Clipboard {
155142
ContextMenuRegistry.registry.register(cutAction);
156143
}
157144

145+
/**
146+
* Precondition for cut and copy context menus. These are similar to the
147+
* ones in core but they don't check if a gesture is in progress,
148+
* because a gesture will always be in progress if the context menu
149+
* is open.
150+
*
151+
* @param scope scope on which the menu was opened.
152+
* @returns 'enabled', 'disabled', or 'hidden' as appropriate
153+
*/
154+
private cutCopyPrecondition(scope: ContextMenuRegistry.Scope): string {
155+
const focused = scope.focusedNode;
156+
157+
if (!focused || !isCopyable(focused)) return 'hidden';
158+
159+
const workspace = focused.workspace;
160+
if (
161+
!workspace.isReadOnly() &&
162+
isDeletable(focused) &&
163+
focused.isDeletable() &&
164+
isDraggable(focused) &&
165+
focused.isMovable() &&
166+
!focused.workspace.isFlyout
167+
)
168+
return 'enabled';
169+
170+
return 'disabled';
171+
}
172+
158173
/**
159174
* The callback for the cut action. Uses the registered version of the cut callback
160175
* to perform the cut logic, then pops a toast if cut happened.
@@ -200,7 +215,7 @@ export class Clipboard {
200215
callback: this.copyCallback.bind(this),
201216
// The registry gives back keycodes as an object instead of an array
202217
// See https://github.com/google/blockly/issues/9008
203-
keyCodes: Object.values(oldCopyShortcut.keyCodes ?? []),
218+
keyCodes: oldCopyShortcut.keyCodes,
204219
allowCollision: false,
205220
};
206221

@@ -218,25 +233,11 @@ export class Clipboard {
218233
private registerCopyContextMenuAction() {
219234
const copyAction: ContextMenuRegistry.RegistryItem = {
220235
displayText: (scope) =>
221-
Msg['COPY_SHORTCUT'].replace('%1', getShortActionShortcut(Constants.SHORTCUT_NAMES.COPY)),
222-
preconditionFn: (scope) => {
223-
const focused = scope.focusedNode;
224-
225-
if (!focused || !isCopyable(focused)) return 'hidden';
226-
227-
const workspace = focused.workspace;
228-
if (
229-
!workspace.isReadOnly() &&
230-
isDeletable(focused) &&
231-
focused.isDeletable() &&
232-
isDraggable(focused) &&
233-
focused.isMovable() &&
234-
!focused.workspace.isFlyout
235-
)
236-
return 'enabled';
237-
238-
return 'disabled';
239-
},
236+
Msg['COPY_SHORTCUT'].replace(
237+
'%1',
238+
getShortActionShortcut(Constants.SHORTCUT_NAMES.COPY),
239+
),
240+
preconditionFn: (scope) => this.cutCopyPrecondition(scope),
240241
callback: (scope, menuOpenEvent) => {
241242
if (!isCopyable(scope.focusedNode)) return false;
242243
const ws = scope.focusedNode.workspace;
@@ -314,7 +315,7 @@ export class Clipboard {
314315
},
315316
// The registry gives back keycodes as an object instead of an array
316317
// See https://github.com/google/blockly/issues/9008
317-
keyCodes: Object.values(oldPasteShortcut.keyCodes ?? []),
318+
keyCodes: oldPasteShortcut.keyCodes,
318319
allowCollision: false,
319320
};
320321

@@ -332,7 +333,10 @@ export class Clipboard {
332333
private registerPasteContextMenuAction() {
333334
const pasteAction: ContextMenuRegistry.RegistryItem = {
334335
displayText: (scope) =>
335-
Msg['PASTE_SHORTCUT'].replace('%1', getShortActionShortcut(Constants.SHORTCUT_NAMES.PASTE)),
336+
Msg['PASTE_SHORTCUT'].replace(
337+
'%1',
338+
getShortActionShortcut(Constants.SHORTCUT_NAMES.PASTE),
339+
),
336340
preconditionFn: (scope: ContextMenuRegistry.Scope) => {
337341
const workspace = this.getPasteWorkspace(scope);
338342
if (!workspace) return 'hidden';

0 commit comments

Comments
 (0)