all repos — snow-editor @ da44ba535c2043b2be1afcd3798276c63ca7e677

small and cozy markdown, and orgmode editor

src/App.jsx (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
import {
  useCallback,
  useDeferredValue,
  useEffect,
  useMemo,
  useRef,
  useState,
} from 'react';
import {
  MODES,
  isDefaultContent,
  loadContent,
  loadMode,
  persistContent,
  persistMode,
} from './lib/editorConstants.js';
import { buildPreviewHtml, ensureMarkedLoaded } from './lib/previewHtml.js';

const STORAGE_DEBOUNCE_MS = 500;
const APP_VERSION = '0.0.1';
const CREATOR_NAME = 'Pablo Murad';
const CREATOR_EMAIL = 'pablomurad@pm.me';

function countWords(text) {
  const trimmed = text.trim();
  if (!trimmed) return 0;
  return trimmed.split(/\s+/).filter(Boolean).length;
}

function getFileExtension(filename) {
  const parts = filename.toLowerCase().split('.');
  if (parts.length < 2) return 'txt';
  return parts[parts.length - 1];
}

function useDividerOrientation() {
  const [orientation, setOrientation] = useState('vertical');

  useEffect(() => {
    const media = window.matchMedia('(max-width: 768px)');
    const update = () => setOrientation(media.matches ? 'horizontal' : 'vertical');
    update();
    media.addEventListener('change', update);
    return () => media.removeEventListener('change', update);
  }, []);

  return orientation;
}

function App() {
  const initialMode = loadMode();
  const [mode, setMode] = useState(initialMode);
  const [content, setContent] = useState(() => loadContent(initialMode));
  const [storageWarning, setStorageWarning] = useState(false);
  const [markedReady, setMarkedReady] = useState(false);
  const fileInputRef = useRef(null);
  const editorRef = useRef(null);
  const dividerOrientation = useDividerOrientation();

  const deferredContent = useDeferredValue(content);
  const previewIsStale = content !== deferredContent;

  useEffect(() => {
    if (mode !== MODES.MARKDOWN) return;
    let cancelled = false;
    ensureMarkedLoaded().then(() => {
      if (!cancelled) setMarkedReady(true);
    });
    return () => {
      cancelled = true;
    };
  }, [mode]);

  useEffect(() => {
    const timer = window.setTimeout(() => {
      const saved = persistContent(mode, content);
      setStorageWarning(!saved);
    }, STORAGE_DEBOUNCE_MS);

    return () => window.clearTimeout(timer);
  }, [content, mode]);

  const html = useMemo(() => {
    return buildPreviewHtml(mode, deferredContent);
  }, [mode, deferredContent, markedReady]);

  const wordCount = useMemo(() => countWords(content), [content]);
  const charCount = content.length;

  const saveLabel = mode === MODES.ORG ? 'Save .org' : 'Save .md';

  const prefetchMarkdown = useCallback(() => {
    ensureMarkedLoaded().then(() => setMarkedReady(true));
  }, []);

  const handleModeChange = useCallback(
    (nextMode) => {
      if (nextMode === mode) return;

      persistContent(mode, content);
      persistMode(nextMode);
      setMode(nextMode);
      setContent(loadContent(nextMode));
      setStorageWarning(false);
      if (nextMode === MODES.MARKDOWN) {
        prefetchMarkdown();
      }
      editorRef.current?.focus();
    },
    [mode, content, prefetchMarkdown],
  );

  const handleSave = useCallback(() => {
    const isOrg = mode === MODES.ORG;
    const blob = new Blob([content], {
      type: isOrg ? 'text/plain;charset=utf-8' : 'text/markdown;charset=utf-8',
    });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = isOrg ? 'document.org' : 'document.md';
    link.click();
    URL.revokeObjectURL(url);
  }, [content, mode]);

  const handleImport = useCallback(
    (event) => {
      const file = event.target.files?.[0];
      if (!file) return;

      const ext = getFileExtension(file.name);
      let targetMode = mode;
      if (ext === 'org') targetMode = MODES.ORG;
      else if (ext === 'md' || ext === 'markdown') targetMode = MODES.MARKDOWN;

      const reader = new FileReader();
      reader.onload = () => {
        const result = reader.result;
        if (typeof result !== 'string') return;

        if (targetMode !== mode) {
          persistContent(mode, content);
          persistMode(targetMode);
          setMode(targetMode);
          if (targetMode === MODES.MARKDOWN) {
            prefetchMarkdown();
          }
        }

        setContent(result);
        persistContent(targetMode, result);
        setStorageWarning(false);
      };
      reader.readAsText(file);
      event.target.value = '';
    },
    [mode, content, prefetchMarkdown],
  );

  const handleClear = useCallback(() => {
    if (!isDefaultContent(mode, content)) {
      const confirmed = window.confirm(
        'Clear the editor and start a blank document? Current content will be replaced.',
      );
      if (!confirmed) return;
    }

    setContent('');
    persistContent(mode, '');
    setStorageWarning(false);
    editorRef.current?.focus();
  }, [mode, content]);

  const editorAriaLabel =
    mode === MODES.ORG ? 'Org-mode editing area' : 'Markdown editing area';

  return (
    <div className="app">
      <header className="app-header">
        <div className="app-header-text">
          <h1 className="app-title">Snow Editor</h1>
          <p className="app-subtitle">Write calmly. See the result live.</p>
          <div className="mode-switch" role="group" aria-label="Editor mode">
            <button
              type="button"
              className={`mode-switch__btn${mode === MODES.MARKDOWN ? ' is-active' : ''}`}
              aria-pressed={mode === MODES.MARKDOWN}
              onClick={() => handleModeChange(MODES.MARKDOWN)}
              onMouseEnter={prefetchMarkdown}
              onFocus={prefetchMarkdown}
            >
              Markdown
            </button>
            <button
              type="button"
              className={`mode-switch__btn${mode === MODES.ORG ? ' is-active' : ''}`}
              aria-pressed={mode === MODES.ORG}
              onClick={() => handleModeChange(MODES.ORG)}
            >
              Org-mode
            </button>
          </div>
        </div>
        <div className="toolbar" role="toolbar" aria-label="Editor actions">
          <button
            type="button"
            className="btn"
            onClick={handleSave}
            aria-label={mode === MODES.ORG ? 'Save as Org-mode file' : 'Save as Markdown file'}
          >
            {saveLabel}
          </button>
          <button
            type="button"
            className="btn"
            onClick={() => fileInputRef.current?.click()}
            aria-label="Import file"
          >
            Import
          </button>
          <input
            ref={fileInputRef}
            id="file-import"
            type="file"
            accept=".md,.markdown,.org,.txt,text/markdown,text/plain"
            className="file-input-hidden"
            onChange={handleImport}
            aria-label="Choose file to import"
          />
          <button
            type="button"
            className="btn btn-ghost"
            onClick={handleClear}
            aria-label="Clear editor and start blank document"
          >
            Clear
          </button>
        </div>
      </header>

      <main className="app-layout">
        <section
          className="panel panel-editor"
          aria-label={mode === MODES.ORG ? 'Org-mode editor' : 'Markdown editor'}
        >
          <div className="panel-label">Write</div>
          <textarea
            ref={editorRef}
            className="editor"
            value={content}
            onChange={(e) => setContent(e.target.value)}
            spellCheck="true"
            aria-label={editorAriaLabel}
            placeholder="Start writing..."
          />
        </section>

        <div
          className="layout-divider"
          role="separator"
          aria-orientation={dividerOrientation}
        />

        <section className="panel panel-preview" aria-label="Document preview">
          <div className="panel-label">Read</div>
          <div
            className={`preview-paper${previewIsStale ? ' preview-updating' : ''}`}
            dangerouslySetInnerHTML={{ __html: html }}
          />
        </section>
      </main>

      <footer className="app-footer">
        <div className="app-footer-stats">
          <span>
            {wordCount} {wordCount === 1 ? 'word' : 'words'}
          </span>
          <span className="footer-separator">·</span>
          <span>
            {charCount} {charCount === 1 ? 'character' : 'characters'}
          </span>
        </div>
        {storageWarning && (
          <p className="app-storage-warning" role="status">
            Draft too large to save in browser storage; export with {saveLabel}.
          </p>
        )}
        <p className="app-meta">
          Snow Editor v{APP_VERSION} · {CREATOR_NAME} ·{' '}
          <a href={`mailto:${CREATOR_EMAIL}`}>{CREATOR_EMAIL}</a>
        </p>
      </footer>
    </div>
  );
}

export default App;