Skip to content

Commit d1d5505

Browse files
committed
Fix toolbar read-only guards
1 parent ef694ea commit d1d5505

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/components/EditorHeader/ControlPanel.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666
import { enterFullscreen, exitFullscreen } from "../../utils/fullscreen";
6767
import { dataURItoBlob } from "../../utils/utils";
6868
import { classifyClipboardPayload } from "../../utils/clipboard";
69+
import { canMutateDiagram } from "../../utils/permissions";
6970
import { IconAddArea, IconAddNote, IconAddTable } from "../../icons";
7071
import LayoutDropdown from "./LayoutDropdown";
7172
import Sidesheet from "./SideSheet/Sidesheet";
@@ -621,7 +622,7 @@ export default function ControlPanel({
621622
}
622623
};
623624
const del = () => {
624-
if (layout.readonly) {
625+
if (!canMutateDiagram(layout)) {
625626
return;
626627
}
627628
switch (selectedElement.element) {
@@ -639,7 +640,7 @@ export default function ControlPanel({
639640
}
640641
};
641642
const duplicate = () => {
642-
if (layout.readonly) {
643+
if (!canMutateDiagram(layout)) {
643644
return;
644645
}
645646
switch (selectedElement.element) {
@@ -699,7 +700,7 @@ export default function ControlPanel({
699700
}
700701
};
701702
const paste = () => {
702-
if (layout.readonly) {
703+
if (!canMutateDiagram(layout)) {
703704
return;
704705
}
705706
navigator.clipboard.readText().then((text) => {
@@ -744,7 +745,7 @@ export default function ControlPanel({
744745
});
745746
};
746747
const cut = () => {
747-
if (layout.readonly) {
748+
if (!canMutateDiagram(layout)) {
748749
return;
749750
}
750751
copy();

src/utils/clipboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Validator } from "jsonschema";
2-
import { tableSchema, areaSchema, noteSchema } from "../data/schemas";
2+
import { tableSchema, areaSchema, noteSchema } from "../data/schemas.js";
33

44
const validator = new Validator();
55

src/utils/permissions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function canMutateDiagram(layout) {
2+
if (!layout) {
3+
return true;
4+
}
5+
6+
return !layout.readOnly;
7+
}
8+
9+

tests/permissions.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
import { canMutateDiagram } from "../src/utils/permissions.js";
4+
5+
test("allows mutations when layout is undefined", () => {
6+
assert.equal(canMutateDiagram(undefined), true);
7+
});
8+
9+
test("blocks mutations when readOnly flag is true", () => {
10+
assert.equal(canMutateDiagram({ readOnly: true }), false);
11+
});
12+
13+
test("allows mutations when readOnly flag is false", () => {
14+
assert.equal(canMutateDiagram({ readOnly: false }), true);
15+
});
16+
17+

0 commit comments

Comments
 (0)