|
| 1 | +import React, { memo } from "react"; |
| 2 | +import { X } from "lucide-react"; |
| 3 | +import { useEditorStore } from "./editorStore"; |
| 4 | +import { ShareDialog } from "./ShareDialog"; |
| 5 | +import { LoadExternalDialog } from "./LoadExternalDialog"; |
| 6 | +import { getTranslations } from "./translations"; |
| 7 | + |
| 8 | +interface EditorDialogsProps { |
| 9 | + defaultLanguage?: string; |
| 10 | + jsonStore?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export const EditorDialogs = memo(({ defaultLanguage = "en", jsonStore = "https://json.openpatch.org" }: EditorDialogsProps) => { |
| 14 | + // Get state from store |
| 15 | + const settings = useEditorStore(state => state.settings); |
| 16 | + const helpOpen = useEditorStore(state => state.helpOpen); |
| 17 | + const pendingExternalId = useEditorStore(state => state.pendingExternalId); |
| 18 | + |
| 19 | + // Get actions from store |
| 20 | + const setHelpOpen = useEditorStore(state => state.setHelpOpen); |
| 21 | + const setShareDialogOpen = useEditorStore(state => state.setShareDialogOpen); |
| 22 | + const setLoadExternalDialogOpen = useEditorStore(state => state.setLoadExternalDialogOpen); |
| 23 | + const setPendingExternalId = useEditorStore(state => state.setPendingExternalId); |
| 24 | + const getRoadmapData = useEditorStore(state => state.getRoadmapData); |
| 25 | + const loadRoadmapData = useEditorStore(state => state.loadRoadmapData); |
| 26 | + |
| 27 | + const language = settings?.language || defaultLanguage; |
| 28 | + const t = getTranslations(language); |
| 29 | + |
| 30 | + const onDownload = () => { |
| 31 | + const roadmapData = getRoadmapData(); |
| 32 | + const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(roadmapData, null, 2)); |
| 33 | + const downloadAnchorNode = document.createElement('a'); |
| 34 | + downloadAnchorNode.setAttribute("href", dataStr); |
| 35 | + downloadAnchorNode.setAttribute("download", "learningmap.json"); |
| 36 | + document.body.appendChild(downloadAnchorNode); |
| 37 | + downloadAnchorNode.click(); |
| 38 | + downloadAnchorNode.remove(); |
| 39 | + }; |
| 40 | + |
| 41 | + const onLoadFromStore = async (id: string) => { |
| 42 | + try { |
| 43 | + const response = await fetch(`${jsonStore}/api/json/${id}`); |
| 44 | + if (!response.ok) throw new Error("Failed to load from JSON store"); |
| 45 | + const data = await response.json(); |
| 46 | + loadRoadmapData(data); |
| 47 | + setLoadExternalDialogOpen(false); |
| 48 | + setPendingExternalId(null); |
| 49 | + } catch (error) { |
| 50 | + console.error("Failed to load from JSON store", error); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const keyboardShortcuts = [ |
| 55 | + { action: t.shortcuts.undo, shortcut: "Ctrl+Z" }, |
| 56 | + { action: t.shortcuts.redo, shortcut: "Ctrl+Y or Ctrl+Shift+Z" }, |
| 57 | + { action: t.shortcuts.addTaskNode, shortcut: "Ctrl+1" }, |
| 58 | + { action: t.shortcuts.addTopicNode, shortcut: "Ctrl+2" }, |
| 59 | + { action: t.shortcuts.addImageNode, shortcut: "Ctrl+3" }, |
| 60 | + { action: t.shortcuts.addTextNode, shortcut: "Ctrl+4" }, |
| 61 | + { action: t.shortcuts.deleteNodeEdge, shortcut: "Delete" }, |
| 62 | + { action: t.shortcuts.togglePreviewMode, shortcut: "Ctrl+P" }, |
| 63 | + { action: t.shortcuts.toggleDebugMode, shortcut: "Ctrl+D" }, |
| 64 | + { action: t.shortcuts.selectMultipleNodes, shortcut: "Ctrl+Click or Shift+Drag" }, |
| 65 | + { action: t.shortcuts.selectAllNodes, shortcut: "Ctrl+A" }, |
| 66 | + { action: t.shortcuts.showHelp, shortcut: "Ctrl+? or Help Button" }, |
| 67 | + { action: t.shortcuts.save, shortcut: "Ctrl+S" }, |
| 68 | + { action: t.shortcuts.zoomIn, shortcut: "Ctrl++" }, |
| 69 | + { action: t.shortcuts.zoomOut, shortcut: "Ctrl+-" }, |
| 70 | + { action: t.shortcuts.resetZoom, shortcut: "Ctrl+0" }, |
| 71 | + { action: t.shortcuts.resetMap, shortcut: "Ctrl+Delete" }, |
| 72 | + { action: t.shortcuts.fitView, shortcut: "Shift+!" }, |
| 73 | + { action: t.shortcuts.zoomToSelection, shortcut: "Shift+@" }, |
| 74 | + { action: t.shortcuts.toggleGrid, shortcut: "Ctrl+'" }, |
| 75 | + { action: t.shortcuts.cut, shortcut: "Ctrl+X" }, |
| 76 | + { action: t.shortcuts.copy, shortcut: "Ctrl+C" }, |
| 77 | + { action: t.shortcuts.paste, shortcut: "Ctrl+V" }, |
| 78 | + ]; |
| 79 | + |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <dialog |
| 83 | + className="help" |
| 84 | + open={helpOpen} |
| 85 | + onClose={() => setHelpOpen(false)} |
| 86 | + > |
| 87 | + <header className="help-header"> |
| 88 | + <h2>{t.keyboardShortcuts}</h2> |
| 89 | + <button className="close-button" onClick={() => setHelpOpen(false)} aria-label={t.close}> |
| 90 | + <X size={20} /> |
| 91 | + </button> |
| 92 | + </header> |
| 93 | + <div className="help-content"> |
| 94 | + <table> |
| 95 | + <thead> |
| 96 | + <tr> |
| 97 | + <th>{t.action}</th> |
| 98 | + <th>{t.shortcut}</th> |
| 99 | + </tr> |
| 100 | + </thead> |
| 101 | + <tbody> |
| 102 | + {keyboardShortcuts.map((item) => ( |
| 103 | + <tr key={item.action}> |
| 104 | + <td>{item.action}</td> |
| 105 | + <td>{item.shortcut}</td> |
| 106 | + </tr> |
| 107 | + ))} |
| 108 | + </tbody> |
| 109 | + </table> |
| 110 | + </div> |
| 111 | + <div className="help-footer"> |
| 112 | + <button className="primary-button" onClick={() => setHelpOpen(false)}>{t.close}</button> |
| 113 | + </div> |
| 114 | + </dialog> |
| 115 | + <ShareDialog /> |
| 116 | + <LoadExternalDialog |
| 117 | + onClose={() => { |
| 118 | + setLoadExternalDialogOpen(false); |
| 119 | + setPendingExternalId(null); |
| 120 | + }} |
| 121 | + onDownloadCurrent={onDownload} |
| 122 | + onReplace={() => { |
| 123 | + if (pendingExternalId) { |
| 124 | + onLoadFromStore(pendingExternalId); |
| 125 | + } |
| 126 | + }} |
| 127 | + /> |
| 128 | + </> |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +EditorDialogs.displayName = 'EditorDialogs'; |
0 commit comments