Add emoji + rich Markdown dual preview, MarkText-style tables, local/S3 images, settings UI, Electron 44, and update README. Co-authored-by: Cursor <cursoragent@cursor.com>
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
import {
|
|
DEFAULT_FOOTER_TEXT,
|
|
DEFAULT_THEME_ID,
|
|
isThemeId,
|
|
type ThemeId,
|
|
} from './emojiMarkdown';
|
|
import { DEFAULT_TEMPLATE_CONTENT } from './templates';
|
|
|
|
export type EditorMode = 'markdown' | 'plain';
|
|
|
|
export type DraftState = {
|
|
content: string;
|
|
appendFooter: boolean;
|
|
footerText: string;
|
|
/** Plain-text (emoji) preview theme */
|
|
themeId: ThemeId;
|
|
/** Long-form article preview theme */
|
|
articleThemeId: ThemeId;
|
|
editorMode: EditorMode;
|
|
tocOpen: boolean;
|
|
};
|
|
|
|
const STORAGE_KEY = 'emojitool.draft';
|
|
|
|
const DEFAULT_DRAFT: DraftState = {
|
|
content: DEFAULT_TEMPLATE_CONTENT,
|
|
appendFooter: true,
|
|
footerText: DEFAULT_FOOTER_TEXT,
|
|
themeId: DEFAULT_THEME_ID,
|
|
articleThemeId: DEFAULT_THEME_ID,
|
|
editorMode: 'markdown',
|
|
tocOpen: true,
|
|
};
|
|
|
|
export function loadDraft(): DraftState {
|
|
try {
|
|
const raw = localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) {
|
|
return { ...DEFAULT_DRAFT };
|
|
}
|
|
const parsed = JSON.parse(raw) as Partial<DraftState>;
|
|
const themeId: ThemeId = isThemeId(parsed.themeId)
|
|
? parsed.themeId
|
|
: DEFAULT_THEME_ID;
|
|
const articleThemeId: ThemeId = isThemeId(parsed.articleThemeId)
|
|
? parsed.articleThemeId
|
|
: themeId;
|
|
const content =
|
|
typeof parsed.content === 'string'
|
|
? parsed.content
|
|
: DEFAULT_TEMPLATE_CONTENT;
|
|
return {
|
|
content,
|
|
appendFooter:
|
|
typeof parsed.appendFooter === 'boolean'
|
|
? parsed.appendFooter
|
|
: true,
|
|
footerText:
|
|
typeof parsed.footerText === 'string' && parsed.footerText.length > 0
|
|
? parsed.footerText
|
|
: DEFAULT_FOOTER_TEXT,
|
|
themeId,
|
|
articleThemeId,
|
|
editorMode: parsed.editorMode === 'plain' ? 'plain' : 'markdown',
|
|
tocOpen: typeof parsed.tocOpen === 'boolean' ? parsed.tocOpen : true,
|
|
};
|
|
} catch {
|
|
return { ...DEFAULT_DRAFT };
|
|
}
|
|
}
|
|
|
|
export function saveDraft(draft: DraftState): void {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(draft));
|
|
} catch {
|
|
// ignore quota / private mode errors
|
|
}
|
|
}
|
|
|
|
export function clearDraft(): void {
|
|
try {
|
|
localStorage.removeItem(STORAGE_KEY);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|