editorConstants.js (2741B)
1 export const STORAGE_MODE = 'editor_mode'; 2 export const STORAGE_MARKDOWN = 'snow_editor_markdown_content'; 3 export const STORAGE_ORG = 'snow_editor_org_content'; 4 export const STORAGE_LEGACY_MARKDOWN = 'cozy-markdown-editor-content'; 5 6 export const MODES = { 7 MARKDOWN: 'markdown', 8 ORG: 'org', 9 }; 10 11 const DEFAULT_MARKDOWN = `# Snow Journal 12 13 Welcome to your quiet writing nook. 14 15 Here you can write in **Markdown** and watch the result appear gently beside you. 16 17 ## Ideas 18 19 - Write notes 20 - Create documents 21 - Draft texts 22 - Organize thoughts 23 24 > A clean space to think calmly. 25 26 \`\`\`js 27 console.log("writing in peace"); 28 \`\`\` 29 `; 30 31 const DEFAULT_ORG = `#+TITLE: Snow Journal 32 #+AUTHOR: Snow Editor 33 34 * Welcome :intro: 35 A quiet Org-mode writing nook with /live preview/. 36 37 ** TODO Ideas :notes: 38 - [ ] Write notes 39 - [X] Try checklist click in the editor 40 - Nested list 41 - Inner item 42 - Another inner item 43 44 ** DONE First table 45 46 | Feature | Status | 47 |-----------+--------| 48 | Tables | yes | 49 | Outline | yes | 50 51 #+BEGIN_QUOTE 52 A clean space to think calmly. 53 #+END_QUOTE 54 55 #+BEGIN_SRC js 56 console.log("writing in peace"); 57 #+END_SRC 58 59 ** Links 60 61 [[https://orgmode.org][Official Org-mode site]] 62 `; 63 64 function getStorageKey(mode) { 65 return mode === MODES.ORG ? STORAGE_ORG : STORAGE_MARKDOWN; 66 } 67 68 function getDefaultContent(mode) { 69 return mode === MODES.ORG ? DEFAULT_ORG : DEFAULT_MARKDOWN; 70 } 71 72 function migrateLegacyStorage() { 73 try { 74 const legacy = localStorage.getItem(STORAGE_LEGACY_MARKDOWN); 75 if (legacy !== null && localStorage.getItem(STORAGE_MARKDOWN) === null) { 76 localStorage.setItem(STORAGE_MARKDOWN, legacy); 77 } 78 } catch { 79 /* ignore */ 80 } 81 } 82 83 export function initStorage() { 84 migrateLegacyStorage(); 85 } 86 87 export function loadMode() { 88 initStorage(); 89 try { 90 const saved = localStorage.getItem(STORAGE_MODE); 91 if (saved === MODES.ORG || saved === MODES.MARKDOWN) { 92 return saved; 93 } 94 } catch { 95 /* ignore */ 96 } 97 return MODES.MARKDOWN; 98 } 99 100 export function loadContent(mode) { 101 try { 102 const saved = localStorage.getItem(getStorageKey(mode)); 103 if (saved === null) { 104 return getDefaultContent(mode); 105 } 106 return saved; 107 } catch { 108 /* ignore */ 109 } 110 return getDefaultContent(mode); 111 } 112 113 export function persistContent(mode, content) { 114 try { 115 localStorage.setItem(getStorageKey(mode), content); 116 return true; 117 } catch (error) { 118 if (error instanceof DOMException && error.name === 'QuotaExceededError') { 119 return false; 120 } 121 return false; 122 } 123 } 124 125 export function persistMode(mode) { 126 try { 127 localStorage.setItem(STORAGE_MODE, mode); 128 } catch { 129 /* ignore */ 130 } 131 } 132 133 export function isDefaultContent(mode, text) { 134 return text === getDefaultContent(mode); 135 }