Files
MetonaEditor/src/index.ts
T
thzxx 708f0937d8
CI / test (18.x) (push) Canceled after 0s
CI / test (20.x) (push) Canceled after 0s
CI / test (22.x) (push) Canceled after 0s
CI / test (24.x) (push) Canceled after 0s
feat: v0.2.3 — 浮动格式工具栏 + 10个新API + 德语翻译 + 类型修复
### Added
- 浮动格式工具栏:选中文本自动弹出 bold/italic/code/link/strikethrough
- 10 个新 API: getSelectedText/getCursorPosition/setCursorPosition/scrollToLine/selectLine/selectAll/replaceAll/replaceAllRegex/lineCount/getLine
- 2 个新事件: selectionChange / cursorMove
- 德语 (de) locale 完整翻译
- floatingToolbar 配置项(默认 true)

### Fixed
- onLinkClick 移除 as any,正式加入 EditorOptions/DEFAULTS
- 所有版本号统一为 0.2.3

### Tests
- core.test.ts: 183 → 214 (+31 tests)
- 总计 ~725 tests
2026-07-25 14:53:43 +08:00

74 lines
4.4 KiB
TypeScript

/**
* MetonaEditor — Type-safe, lightweight Markdown Editor
* @module metona-editor
* @version 0.2.3
*/
import { MarkdownEditor } from './core';
import { parseMarkdown, parseTokens, renderTokens, safeUrl, slugify, clearRenderCache, registerBlockHandler } from './parser';
import { themeUtils, exportCSSVars, getCSSVariable, followExternalTheme, adoptFromParent, createInstanceTheme } from './themes';
import { i18nUtils, createInstanceI18n, loadRemote } from './i18n';
import { pluginUtils, presetPlugins, topologicalSort, validateConfig } from './plugins';
import { animationUtils } from './animations';
import { DEFAULTS, ICONS, THEMES, EDIT_MODES, DEFAULT_TOOLBAR, TOOLBAR_ACTIONS } from './constants';
import type { EditMode, ThemeName, ToolbarItem, EditorOptions } from './constants';
const VERSION = '0.2.3';
const globalPlugins: any[] = [];
MarkdownEditor.on('beforeCreate', (editor) => {
if (!editor || editor.isDestroyed()) return;
globalPlugins.forEach((p) => { try { editor.use(p); } catch (e) { console.error('MeEditor global plugin install error:', e); } });
});
function create(container: string | HTMLElement, options: EditorOptions = {}): MarkdownEditor {
return new MarkdownEditor(container, options);
}
function use(plugin: string | any, options: any = {}): typeof api {
let p = plugin;
if (typeof plugin === 'string') { p = presetPlugins[plugin]; if (!p) { console.warn(`MeEditor: preset plugin "${plugin}" not found`); return api; } }
if (!p || typeof p !== 'object') { console.warn('MeEditor: invalid plugin'); return api; }
const merged = (options && typeof options === 'object' && Object.keys(options).length > 0) ? { ...p, ...options } : p;
globalPlugins.push(merged); return api;
}
function on(name: string, fn: (editor: MarkdownEditor) => void): () => void { return MarkdownEditor.on(name, fn); }
function off(name: string, fn: (editor: MarkdownEditor) => void): typeof api { MarkdownEditor.off(name, fn); return api; }
function setTheme(theme: ThemeName, _options?: any): typeof api { if (themeUtils && typeof themeUtils.switchTheme === 'function') themeUtils.switchTheme(theme); return api; }
function setLocale(locale: string): typeof api { if (i18nUtils && typeof i18nUtils.setCurrentLocale === 'function') i18nUtils.setCurrentLocale(locale); return api; }
function destroy(): void {
if (themeUtils) { try { themeUtils.clearThemeListeners(); } catch (_) {} try { themeUtils.unwatchSystemTheme(); } catch (_) {} }
if (i18nUtils) { try { i18nUtils.clearLocaleListeners(); } catch (_) {} }
if (animationUtils) { try { animationUtils.cancelAll(); } catch (_) {} }
globalPlugins.length = 0;
}
function getStatus() {
return { version: VERSION, theme: (themeUtils && typeof themeUtils.getCurrentTheme === 'function') ? themeUtils.getCurrentTheme() : 'auto', locale: (i18nUtils && typeof i18nUtils.getCurrentLocale === 'function') ? i18nUtils.getCurrentLocale() : 'zh-CN', globalPlugins: globalPlugins.map((p) => p.name || 'custom'), presetPlugins: Object.keys(presetPlugins || {}), };
}
try {
if (typeof themeUtils === 'object' && typeof themeUtils.initTheme === 'function') themeUtils.initTheme();
if (typeof i18nUtils === 'object' && typeof i18nUtils.initI18n === 'function') i18nUtils.initI18n();
} catch (_) {}
const api = {
VERSION, version: VERSION,
MarkdownEditor, Editor: MarkdownEditor,
create, use, on, off, setTheme, setLocale, destroy, getStatus,
parseMarkdown, parseTokens, renderTokens, safeUrl, slugify, clearRenderCache, registerBlockHandler,
themes: themeUtils, i18n: i18nUtils, animations: animationUtils, plugins: pluginUtils, presetPlugins,
topologicalSort, validateConfig, createInstanceI18n, loadRemote,
exportCSSVars, getCSSVariable, followExternalTheme, adoptFromParent, createInstanceTheme,
DEFAULTS, ICONS, THEMES, EDIT_MODES, DEFAULT_TOOLBAR, TOOLBAR_ACTIONS,
};
if (typeof window !== 'undefined') { (window as any).MeEditor = api; }
export default api;
export { api, api as meEditor, api as MeEditor, MarkdownEditor, MarkdownEditor as Editor, create, use, on, off, setTheme, setLocale, destroy, getStatus, parseMarkdown, parseTokens, renderTokens, safeUrl, slugify, clearRenderCache, registerBlockHandler, themeUtils, i18nUtils, pluginUtils, presetPlugins, animationUtils, topologicalSort, validateConfig, createInstanceI18n, loadRemote, exportCSSVars, getCSSVariable, followExternalTheme, adoptFromParent, createInstanceTheme, DEFAULTS, ICONS, THEMES, EDIT_MODES, DEFAULT_TOOLBAR, TOOLBAR_ACTIONS, VERSION, };