Skip to content

Commit dc29d9b

Browse files
committed
disable menubar items when in read only mode
1 parent 35941d2 commit dc29d9b

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/components/EditorHeader/ControlPanel.jsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,9 @@ export default function ControlPanel({
615615
}
616616
};
617617
const del = () => {
618+
if (layout.readonly) {
619+
return;
620+
}
618621
switch (selectedElement.element) {
619622
case ObjectType.TABLE:
620623
deleteTable(selectedElement.id);
@@ -630,6 +633,9 @@ export default function ControlPanel({
630633
}
631634
};
632635
const duplicate = () => {
636+
if (layout.readonly) {
637+
return;
638+
}
633639
switch (selectedElement.element) {
634640
case ObjectType.TABLE: {
635641
const copiedTable = tables.find((t) => t.id === selectedElement.id);
@@ -685,6 +691,9 @@ export default function ControlPanel({
685691
}
686692
};
687693
const paste = () => {
694+
if (layout.readonly) {
695+
return;
696+
}
688697
navigator.clipboard.readText().then((text) => {
689698
let obj = null;
690699
try {
@@ -718,6 +727,9 @@ export default function ControlPanel({
718727
});
719728
};
720729
const cut = () => {
730+
if (layout.readonly) {
731+
return;
732+
}
721733
copy();
722734
del();
723735
};
@@ -747,10 +759,12 @@ export default function ControlPanel({
747759
save: {
748760
function: save,
749761
shortcut: "Ctrl+S",
762+
disabled: layout.readOnly,
750763
},
751764
save_as: {
752765
function: saveDiagramAs,
753766
shortcut: "Ctrl+Shift+S",
767+
disabled: layout.readOnly,
754768
},
755769
save_as_template: {
756770
function: () => {
@@ -775,6 +789,7 @@ export default function ControlPanel({
775789
function: () => {
776790
setModal(MODAL.RENAME);
777791
},
792+
disabled: layout.readOnly,
778793
},
779794
delete_diagram: {
780795
warning: {
@@ -805,13 +820,15 @@ export default function ControlPanel({
805820
{
806821
function: fileImport,
807822
name: "JSON",
823+
disabled: layout.readOnly,
808824
},
809825
{
810826
function: () => {
811827
setModal(MODAL.IMPORT);
812828
setImportFrom(IMPORT_FROM.DBML);
813829
},
814830
name: "DBML",
831+
disabled: layout.readOnly,
815832
},
816833
],
817834
},
@@ -824,34 +841,39 @@ export default function ControlPanel({
824841
setImportDb(DB.MYSQL);
825842
},
826843
name: "MySQL",
844+
disabled: layout.readOnly,
827845
},
828846
{
829847
function: () => {
830848
setModal(MODAL.IMPORT_SRC);
831849
setImportDb(DB.POSTGRES);
832850
},
833851
name: "PostgreSQL",
852+
disabled: layout.readOnly,
834853
},
835854
{
836855
function: () => {
837856
setModal(MODAL.IMPORT_SRC);
838857
setImportDb(DB.SQLITE);
839858
},
840859
name: "SQLite",
860+
disabled: layout.readOnly,
841861
},
842862
{
843863
function: () => {
844864
setModal(MODAL.IMPORT_SRC);
845865
setImportDb(DB.MARIADB);
846866
},
847867
name: "MariaDB",
868+
disabled: layout.readOnly,
848869
},
849870
{
850871
function: () => {
851872
setModal(MODAL.IMPORT_SRC);
852873
setImportDb(DB.MSSQL);
853874
},
854875
name: "MSSQL",
876+
disabled: layout.readOnly,
855877
},
856878
{
857879
function: () => {
@@ -860,6 +882,7 @@ export default function ControlPanel({
860882
},
861883
name: "Oracle",
862884
label: "Beta",
885+
disabled: layout.readOnly,
863886
},
864887
],
865888
}),
@@ -868,6 +891,7 @@ export default function ControlPanel({
868891

869892
setModal(MODAL.IMPORT_SRC);
870893
},
894+
disabled: layout.readOnly,
871895
},
872896
export_source: {
873897
...(database === DB.GENERIC && {
@@ -1159,10 +1183,12 @@ export default function ControlPanel({
11591183
undo: {
11601184
function: undo,
11611185
shortcut: "Ctrl+Z",
1186+
disabled: layout.readOnly || undoStack.length === 0,
11621187
},
11631188
redo: {
11641189
function: redo,
11651190
shortcut: "Ctrl+Y",
1191+
disabled: layout.readOnly || redoStack.length === 0,
11661192
},
11671193
clear: {
11681194
warning: {
@@ -1194,14 +1220,17 @@ export default function ControlPanel({
11941220
);
11951221
});
11961222
},
1223+
disabled: layout.readOnly,
11971224
},
11981225
edit: {
11991226
function: edit,
12001227
shortcut: "Ctrl+E",
1228+
disabled: layout.readOnly,
12011229
},
12021230
cut: {
12031231
function: cut,
12041232
shortcut: "Ctrl+X",
1233+
disabled: layout.readOnly,
12051234
},
12061235
copy: {
12071236
function: copy,
@@ -1210,14 +1239,17 @@ export default function ControlPanel({
12101239
paste: {
12111240
function: paste,
12121241
shortcut: "Ctrl+V",
1242+
disabled: layout.readOnly,
12131243
},
12141244
duplicate: {
12151245
function: duplicate,
12161246
shortcut: "Ctrl+D",
1247+
disabled: layout.readOnly,
12171248
},
12181249
delete: {
12191250
function: del,
12201251
shortcut: "Del",
1252+
disabled: layout.readOnly,
12211253
},
12221254
copy_as_image: {
12231255
function: copyAsImage,
@@ -1762,7 +1794,7 @@ export default function ControlPanel({
17621794
// https://stackoverflow.com/a/70976017/1137077
17631795
e.target.releasePointerCapture(e.pointerId);
17641796
}}
1765-
onClick={() => setModal(MODAL.RENAME)}
1797+
onClick={!layout.readOnly && (() => setModal(MODAL.RENAME))}
17661798
>
17671799
<span>
17681800
{(window.name.split(" ")[0] === "t"
@@ -1775,7 +1807,7 @@ export default function ControlPanel({
17751807
</Tag>
17761808
)}
17771809
</div>
1778-
{(showEditName || modal === MODAL.RENAME) && <IconEdit />}
1810+
{(showEditName || modal === MODAL.RENAME) && !layout.readOnly && <IconEdit />}
17791811
</div>
17801812
<div className="flex items-center">
17811813
<div className="flex justify-start text-md select-none me-2">
@@ -1804,6 +1836,7 @@ export default function ControlPanel({
18041836
key={i}
18051837
onClick={e.function}
18061838
className="flex justify-between"
1839+
disabled={e.disabled}
18071840
>
18081841
<span>{e.name}</span>
18091842
{e.label && (
@@ -1857,6 +1890,7 @@ export default function ControlPanel({
18571890
return (
18581891
<Dropdown.Item
18591892
key={index}
1893+
disabled={menu[category][item].disabled}
18601894
onClick={menu[category][item].function}
18611895
style={
18621896
menu[category][item].shortcut && {

src/components/EditorHeader/SideSheet/Versions.jsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export default function Versions({ open, title, setTitle }) {
9595
setVersion(sha);
9696
setLayout((prev) => ({ ...prev, readOnly: true }));
9797

98+
if (!version.data.files[VERSION_FILENAME]) {
99+
return;
100+
}
101+
98102
const content = version.data.files[VERSION_FILENAME].content;
99103
const parsedDiagram = JSON.parse(content);
100104

@@ -112,10 +116,11 @@ export default function Versions({ open, title, setTitle }) {
112116
setEnums(parsedDiagram.enums);
113117
}
114118
} catch (e) {
115-
Toast.error("failed_to_load_diagram");
119+
Toast.error(t("failed_to_load_diagram"));
116120
}
117121
},
118122
[
123+
t,
119124
gistId,
120125
setTables,
121126
setRelationships,
@@ -178,6 +183,11 @@ export default function Versions({ open, title, setTitle }) {
178183
if (!gistId) return true;
179184

180185
const previousVersion = await get(gistId);
186+
187+
if (!previousVersion.data.files[VERSION_FILENAME]) {
188+
return true;
189+
}
190+
181191
const previousDiagram = JSON.parse(
182192
previousVersion.data.files[VERSION_FILENAME]?.content,
183193
);
@@ -209,15 +219,14 @@ export default function Versions({ open, title, setTitle }) {
209219
} else {
210220
const id = await create(VERSION_FILENAME, diagramToString());
211221
setGistId(id);
212-
console.log("new gist created", id);
213222
}
214223

215224
delete cacheRef[gistId];
216225
saveCache(cacheRef);
217226

218227
await getRevisions();
219228
} catch (e) {
220-
Toast.error("failed_to_record_version");
229+
Toast.error(t("failed_to_record_version"));
221230
} finally {
222231
setIsRecording(false);
223232
}

src/i18n/locales/en.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ const en = {
274274
load_more: "Load more",
275275
clear_cache: "Clear cache",
276276
cache_cleared: "Cache cleared",
277-
failed_to_record_version: "Failed to record version"
277+
failed_to_record_version: "Failed to record version",
278+
failed_to_load_diagram: "Failed to load diagram",
278279
},
279280
};
280281

0 commit comments

Comments
 (0)