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
6 changes: 3 additions & 3 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ export class Block implements IASTNodeLocation {
this.deletable &&
!this.shadow &&
!this.isDeadOrDying() &&
!this.workspace.options.readOnly
!this.workspace.isReadOnly()
);
}

Expand Down Expand Up @@ -828,7 +828,7 @@ export class Block implements IASTNodeLocation {
this.movable &&
!this.shadow &&
!this.isDeadOrDying() &&
!this.workspace.options.readOnly
!this.workspace.isReadOnly()
);
}

Expand Down Expand Up @@ -917,7 +917,7 @@ export class Block implements IASTNodeLocation {
*/
isEditable(): boolean {
return (
this.editable && !this.isDeadOrDying() && !this.workspace.options.readOnly
this.editable && !this.isDeadOrDying() && !this.workspace.isReadOnly()
);
}

Expand Down
6 changes: 4 additions & 2 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class BlockSvg
this.applyColour();
this.pathObject.updateMovable(this.isMovable() || this.isInFlyout);
const svg = this.getSvgRoot();
if (!this.workspace.options.readOnly && svg) {
if (svg) {
browserEvents.conditionalBind(svg, 'pointerdown', this, this.onMouseDown);
}

Expand Down Expand Up @@ -585,6 +585,8 @@ export class BlockSvg
* @param e Pointer down event.
*/
private onMouseDown(e: PointerEvent) {
if (this.workspace.isReadOnly()) return;

const gesture = this.workspace.getGesture(e);
if (gesture) {
gesture.handleBlockStart(e, this);
Expand Down Expand Up @@ -612,7 +614,7 @@ export class BlockSvg
protected generateContextMenu(): Array<
ContextMenuOption | LegacyContextMenuOption
> | null {
if (this.workspace.options.readOnly || !this.contextMenu) {
if (this.workspace.isReadOnly() || !this.contextMenu) {
return null;
}
const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions(
Expand Down
6 changes: 3 additions & 3 deletions core/comments/workspace_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class WorkspaceComment {
* workspace is read-only.
*/
isEditable(): boolean {
return this.isOwnEditable() && !this.workspace.options.readOnly;
return this.isOwnEditable() && !this.workspace.isReadOnly();
}

/**
Expand All @@ -165,7 +165,7 @@ export class WorkspaceComment {
* workspace is read-only.
*/
isMovable() {
return this.isOwnMovable() && !this.workspace.options.readOnly;
return this.isOwnMovable() && !this.workspace.isReadOnly();
}

/**
Expand All @@ -189,7 +189,7 @@ export class WorkspaceComment {
return (
this.isOwnDeletable() &&
!this.isDeadOrDying() &&
!this.workspace.options.readOnly
!this.workspace.isReadOnly()
);
}

Expand Down
2 changes: 1 addition & 1 deletion core/dragging/block_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class BlockDragStrategy implements IDragStrategy {
return (
this.block.isOwnMovable() &&
!this.block.isDeadOrDying() &&
!this.workspace.options.readOnly &&
!this.workspace.isReadOnly() &&
// We never drag blocks in the flyout, only create new blocks that are
// dragged.
!this.block.isInFlyout
Expand Down
2 changes: 1 addition & 1 deletion core/dragging/comment_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CommentDragStrategy implements IDragStrategy {
return (
this.comment.isOwnMovable() &&
!this.comment.isDeadOrDying() &&
!this.workspace.options.readOnly
!this.workspace.isReadOnly()
);
}

Expand Down
2 changes: 1 addition & 1 deletion core/flyout_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ export abstract class Flyout
* @internal
*/
isBlockCreatable(block: BlockSvg): boolean {
return block.isEnabled();
return block.isEnabled() && !this.getTargetWorkspace().isReadOnly();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ export class Gesture {
'Cannot do a block click because the target block is ' + 'undefined',
);
}
if (this.targetBlock.isEnabled()) {
if (this.flyout.isBlockCreatable(this.targetBlock)) {
if (!eventUtils.getGroup()) {
eventUtils.setGroup(true);
}
Expand Down
14 changes: 7 additions & 7 deletions core/shortcut_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function registerEscape() {
const escapeAction: KeyboardShortcut = {
name: names.ESCAPE,
preconditionFn(workspace) {
return !workspace.options.readOnly;
return !workspace.isReadOnly();
},
callback(workspace) {
// AnyDuringMigration because: Property 'hideChaff' does not exist on
Expand All @@ -62,7 +62,7 @@ export function registerDelete() {
preconditionFn(workspace) {
const selected = common.getSelected();
return (
!workspace.options.readOnly &&
!workspace.isReadOnly() &&
selected != null &&
isDeletable(selected) &&
selected.isDeletable() &&
Expand Down Expand Up @@ -113,7 +113,7 @@ export function registerCopy() {
preconditionFn(workspace) {
const selected = common.getSelected();
return (
!workspace.options.readOnly &&
!workspace.isReadOnly() &&
!Gesture.inProgress() &&
selected != null &&
isDeletable(selected) &&
Expand Down Expand Up @@ -164,7 +164,7 @@ export function registerCut() {
preconditionFn(workspace) {
const selected = common.getSelected();
return (
!workspace.options.readOnly &&
!workspace.isReadOnly() &&
!Gesture.inProgress() &&
selected != null &&
isDeletable(selected) &&
Expand Down Expand Up @@ -221,7 +221,7 @@ export function registerPaste() {
const pasteShortcut: KeyboardShortcut = {
name: names.PASTE,
preconditionFn(workspace) {
return !workspace.options.readOnly && !Gesture.inProgress();
return !workspace.isReadOnly() && !Gesture.inProgress();
},
callback() {
if (!copyData || !copyWorkspace) return false;
Expand Down Expand Up @@ -269,7 +269,7 @@ export function registerUndo() {
const undoShortcut: KeyboardShortcut = {
name: names.UNDO,
preconditionFn(workspace) {
return !workspace.options.readOnly && !Gesture.inProgress();
return !workspace.isReadOnly() && !Gesture.inProgress();
},
callback(workspace, e) {
// 'z' for undo 'Z' is for redo.
Expand Down Expand Up @@ -308,7 +308,7 @@ export function registerRedo() {
const redoShortcut: KeyboardShortcut = {
name: names.REDO,
preconditionFn(workspace) {
return !Gesture.inProgress() && !workspace.options.readOnly;
return !Gesture.inProgress() && !workspace.isReadOnly();
},
callback(workspace, e) {
// 'z' for undo 'Z' is for redo.
Expand Down
22 changes: 22 additions & 0 deletions core/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class Workspace implements IASTNodeLocation {
private readonly typedBlocksDB = new Map<string, Block[]>();
private variableMap: IVariableMap<IVariableModel<IVariableState>>;
private procedureMap: IProcedureMap = new ObservableProcedureMap();
private readOnly = false;

/**
* Blocks in the flyout can refer to variables that don't exist in the main
Expand Down Expand Up @@ -153,6 +154,8 @@ export class Workspace implements IASTNodeLocation {
*/
const VariableMap = this.getVariableMapClass();
this.variableMap = new VariableMap(this);

this.setIsReadOnly(this.options.readOnly);
}

/**
Expand Down Expand Up @@ -947,4 +950,23 @@ export class Workspace implements IASTNodeLocation {
}
return VariableMap;
}

/**
* Returns whether or not this workspace is in readonly mode.
*
* @returns True if the workspace is readonly, otherwise false.
*/
isReadOnly(): boolean {
return this.readOnly;
}

/**
* Sets whether or not this workspace is in readonly mode.
*
* @param readOnly True to make the workspace readonly, otherwise false.
*/
setIsReadOnly(readOnly: boolean) {
this.readOnly = readOnly;
this.options.readOnly = readOnly;
}
}
11 changes: 10 additions & 1 deletion core/workspace_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg {
* @internal
*/
showContextMenu(e: PointerEvent) {
if (this.options.readOnly || this.isFlyout) {
if (this.isReadOnly() || this.isFlyout) {
return;
}
const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions(
Expand Down Expand Up @@ -2532,6 +2532,15 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg {
dom.removeClass(this.injectionDiv, className);
}
}

override setIsReadOnly(readOnly: boolean) {
super.setIsReadOnly(readOnly);
if (readOnly) {
this.addClass('blocklyReadOnly');
} else {
this.removeClass('blocklyReadOnly');
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/keydown_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ suite('Key Down', function () {
function runReadOnlyTest(keyEvent, opt_name) {
const name = opt_name ? opt_name : 'Not called when readOnly is true';
test(name, function () {
this.workspace.options.readOnly = true;
this.workspace.setIsReadOnly(true);
this.injectionDiv.dispatchEvent(keyEvent);
sinon.assert.notCalled(this.hideChaffSpy);
});
Expand Down