2026-04-28 09:59:30 -07:00
|
|
|
/*
|
|
|
|
|
* ribbit.ts — core editor classes for the ribbit WYSIWYG markdown editor.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-04-28 18:17:32 -07:00
|
|
|
import { HopDown } from './hopdown';
|
|
|
|
|
import { defaultTheme } from './default-theme';
|
|
|
|
|
import { ThemeManager } from './theme-manager';
|
2026-04-28 18:35:06 -07:00
|
|
|
import { RibbitEmitter, type RibbitEventMap } from './events';
|
2026-04-28 20:18:19 -07:00
|
|
|
import { type MacroDef } from './macros';
|
2026-04-28 23:08:20 -07:00
|
|
|
import { ToolbarManager } from './toolbar';
|
|
|
|
|
import type { RibbitTheme, ToolbarSlot } from './types';
|
2026-04-28 09:59:30 -07:00
|
|
|
|
|
|
|
|
export interface RibbitSettings {
|
|
|
|
|
api?: unknown;
|
|
|
|
|
editorId?: string;
|
2026-04-28 18:17:32 -07:00
|
|
|
currentTheme?: string;
|
|
|
|
|
themes?: RibbitTheme[];
|
|
|
|
|
themesPath?: string;
|
2026-04-28 20:03:58 -07:00
|
|
|
macros?: MacroDef[];
|
2026-04-28 23:08:20 -07:00
|
|
|
toolbar?: ToolbarSlot[];
|
|
|
|
|
/** Set to false to prevent auto-rendering the toolbar. Default true. */
|
|
|
|
|
autoToolbar?: boolean;
|
2026-04-28 18:35:06 -07:00
|
|
|
on?: Partial<RibbitEventMap>;
|
2026-04-28 09:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read-only markdown viewer. Renders markdown content into an HTML element.
|
|
|
|
|
*/
|
|
|
|
|
export class Ribbit {
|
|
|
|
|
api: unknown;
|
|
|
|
|
element: HTMLElement;
|
|
|
|
|
states: Record<string, string>;
|
|
|
|
|
cachedHTML: string | null;
|
|
|
|
|
cachedMarkdown: string | null;
|
|
|
|
|
state: string | null;
|
|
|
|
|
changed: boolean;
|
2026-04-28 18:17:32 -07:00
|
|
|
theme: RibbitTheme;
|
|
|
|
|
themes: ThemeManager;
|
|
|
|
|
converter: HopDown;
|
|
|
|
|
themesPath: string;
|
2026-04-28 23:08:20 -07:00
|
|
|
toolbar: ToolbarManager;
|
|
|
|
|
protected autoToolbar: boolean;
|
2026-04-28 18:35:06 -07:00
|
|
|
private emitter: RibbitEmitter;
|
2026-04-28 20:03:58 -07:00
|
|
|
private macros: MacroDef[];
|
2026-04-28 09:59:30 -07:00
|
|
|
|
|
|
|
|
constructor(settings: RibbitSettings) {
|
|
|
|
|
this.api = settings.api || null;
|
|
|
|
|
this.element = document.getElementById(settings.editorId || 'ribbit')!;
|
2026-04-28 18:17:32 -07:00
|
|
|
this.themesPath = settings.themesPath || './themes';
|
2026-04-28 18:35:06 -07:00
|
|
|
this.emitter = new RibbitEmitter();
|
2026-04-28 20:03:58 -07:00
|
|
|
this.macros = settings.macros || [];
|
2026-04-28 09:59:30 -07:00
|
|
|
this.states = {
|
|
|
|
|
VIEW: 'view',
|
|
|
|
|
};
|
|
|
|
|
this.cachedHTML = null;
|
|
|
|
|
this.cachedMarkdown = null;
|
|
|
|
|
this.state = null;
|
|
|
|
|
this.changed = false;
|
|
|
|
|
|
2026-04-28 18:35:06 -07:00
|
|
|
this.themes = new ThemeManager(defaultTheme, this.themesPath, (theme, previous) => {
|
2026-04-28 18:17:32 -07:00
|
|
|
this.theme = theme;
|
|
|
|
|
this.converter = theme.tags
|
2026-04-28 20:03:58 -07:00
|
|
|
? new HopDown({ tags: theme.tags, macros: this.macros })
|
|
|
|
|
: new HopDown({ macros: this.macros });
|
2026-04-28 18:17:32 -07:00
|
|
|
this.cachedHTML = null;
|
2026-04-28 18:35:06 -07:00
|
|
|
this.emitter.emit('themeChange', {
|
|
|
|
|
current: theme,
|
|
|
|
|
previous,
|
|
|
|
|
});
|
2026-04-28 18:17:32 -07:00
|
|
|
if (this.getState() === this.states.VIEW) {
|
|
|
|
|
this.state = null;
|
|
|
|
|
this.view();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
(settings.themes || []).forEach(theme => {
|
|
|
|
|
this.themes.add(theme);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const activeName = settings.currentTheme || defaultTheme.name;
|
|
|
|
|
this.themes.set(activeName);
|
|
|
|
|
this.theme = this.themes.current();
|
|
|
|
|
this.converter = this.theme.tags
|
2026-04-28 20:03:58 -07:00
|
|
|
? new HopDown({ tags: this.theme.tags, macros: this.macros })
|
|
|
|
|
: new HopDown({ macros: this.macros });
|
2026-04-28 18:17:32 -07:00
|
|
|
|
2026-04-28 18:35:06 -07:00
|
|
|
if (settings.on) {
|
|
|
|
|
for (const [event, handler] of Object.entries(settings.on)) {
|
|
|
|
|
if (handler) {
|
|
|
|
|
this.on(event as keyof RibbitEventMap, handler as any);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-28 23:08:20 -07:00
|
|
|
|
|
|
|
|
this.toolbar = new ToolbarManager(
|
|
|
|
|
this,
|
|
|
|
|
this.theme.tags || {},
|
|
|
|
|
this.macros,
|
|
|
|
|
settings.toolbar,
|
|
|
|
|
);
|
|
|
|
|
this.autoToolbar = settings.autoToolbar !== false;
|
2026-04-28 18:35:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
on<K extends keyof RibbitEventMap>(event: K, callback: RibbitEventMap[K]): void {
|
|
|
|
|
this.emitter.on(event, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
off<K extends keyof RibbitEventMap>(event: K, callback: RibbitEventMap[K]): void {
|
|
|
|
|
this.emitter.off(event, callback);
|
2026-04-28 09:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run(): void {
|
|
|
|
|
this.element.classList.add('loaded');
|
2026-04-28 23:08:20 -07:00
|
|
|
if (this.autoToolbar) {
|
|
|
|
|
this.element.parentNode?.insertBefore(this.toolbar.render(), this.element);
|
|
|
|
|
}
|
2026-04-28 09:59:30 -07:00
|
|
|
this.view();
|
2026-04-28 18:35:06 -07:00
|
|
|
this.emitter.emit('ready', {
|
|
|
|
|
markdown: this.getMarkdown(),
|
|
|
|
|
html: this.getHTML(),
|
|
|
|
|
mode: this.state || 'view',
|
|
|
|
|
theme: this.theme,
|
|
|
|
|
});
|
2026-04-28 09:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getState(): string | null {
|
|
|
|
|
return this.state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setState(newState: string): void {
|
2026-04-28 18:35:06 -07:00
|
|
|
const previous = this.state;
|
2026-04-28 20:18:19 -07:00
|
|
|
if (previous) {
|
|
|
|
|
this.element.classList.remove(previous);
|
|
|
|
|
}
|
2026-04-28 09:59:30 -07:00
|
|
|
this.state = newState;
|
2026-04-28 20:18:19 -07:00
|
|
|
this.element.classList.add(newState);
|
2026-04-28 18:35:06 -07:00
|
|
|
this.emitter.emit('modeChange', {
|
|
|
|
|
current: newState,
|
|
|
|
|
previous,
|
|
|
|
|
});
|
2026-04-28 09:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markdownToHTML(md: string): string {
|
2026-04-28 18:17:32 -07:00
|
|
|
return this.converter.toHTML(md);
|
2026-04-28 09:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getHTML(): string {
|
2026-04-28 20:18:19 -07:00
|
|
|
if (this.cachedHTML === null) {
|
2026-04-28 09:59:30 -07:00
|
|
|
this.cachedHTML = this.markdownToHTML(this.getMarkdown());
|
|
|
|
|
}
|
|
|
|
|
return this.cachedHTML;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMarkdown(): string {
|
2026-04-28 20:18:19 -07:00
|
|
|
if (this.cachedMarkdown === null) {
|
2026-04-28 09:59:30 -07:00
|
|
|
this.cachedMarkdown = this.element.textContent || '';
|
|
|
|
|
}
|
|
|
|
|
return this.cachedMarkdown;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 18:35:06 -07:00
|
|
|
save(): void {
|
|
|
|
|
this.emitter.emit('save', {
|
|
|
|
|
markdown: this.getMarkdown(),
|
|
|
|
|
html: this.getHTML(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 09:59:30 -07:00
|
|
|
view(): void {
|
|
|
|
|
if (this.getState() === this.states.VIEW) return;
|
|
|
|
|
this.element.innerHTML = this.getHTML();
|
|
|
|
|
this.setState(this.states.VIEW);
|
|
|
|
|
this.element.contentEditable = 'false';
|
|
|
|
|
}
|
2026-04-28 18:35:06 -07:00
|
|
|
|
2026-04-28 20:18:19 -07:00
|
|
|
invalidateCache(): void {
|
|
|
|
|
this.changed = true;
|
|
|
|
|
this.cachedMarkdown = null;
|
|
|
|
|
this.cachedHTML = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 18:35:06 -07:00
|
|
|
notifyChange(): void {
|
|
|
|
|
this.emitter.emit('change', {
|
|
|
|
|
markdown: this.getMarkdown(),
|
|
|
|
|
html: this.getHTML(),
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-28 09:59:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function camelCase(words: string): string[] {
|
|
|
|
|
return words.trim().split(/\s+/g).map(word => {
|
|
|
|
|
const lc = word.toLowerCase();
|
|
|
|
|
return lc.charAt(0).toUpperCase() + lc.slice(1);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function decodeHtmlEntities(html: string): string {
|
|
|
|
|
const txt = document.createElement('textarea');
|
|
|
|
|
txt.innerHTML = html;
|
|
|
|
|
return txt.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function encodeHtmlEntities(str: string): string {
|
|
|
|
|
return str.replace(/[\u00A0-\u9999<>&]/g, i => '&#' + i.charCodeAt(0) + ';');
|
|
|
|
|
}
|