md-editor-lite 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,410 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ EditorHeader: () => EditorHeader,
34
+ EditorTab: () => EditorTab,
35
+ EditorTextarea: () => EditorTextarea,
36
+ MarkdownEditor: () => MarkdownEditor,
37
+ Preview: () => Preview,
38
+ TOOLBAR_BUTTONS: () => TOOLBAR_BUTTONS,
39
+ Toolbar: () => Toolbar,
40
+ ToolbarDropdownHeading: () => ToolbarDropdownHeading,
41
+ ToolbarDropdownList: () => ToolbarDropdownList,
42
+ cx: () => cx,
43
+ headingOptions: () => headingOptions,
44
+ listOptions: () => listOptions,
45
+ markdownToHtml: () => markdownToHtml,
46
+ useMarkdownEditor: () => useMarkdownEditor
47
+ });
48
+ module.exports = __toCommonJS(index_exports);
49
+
50
+ // src/components/EditorHeader.tsx
51
+ var import_jsx_runtime = require("react/jsx-runtime");
52
+ function EditorHeader({
53
+ mode,
54
+ setMode,
55
+ insertMarkdown
56
+ }) {
57
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("header", { className: "editor-header", children: [
58
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EditorTab, { mode, setMode }),
59
+ mode === "write" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Toolbar, { insertMarkdown })
60
+ ] });
61
+ }
62
+
63
+ // src/components/EditorTab.tsx
64
+ var import_jsx_runtime2 = require("react/jsx-runtime");
65
+ function EditorTab({ mode, setMode }) {
66
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "editor-tab-container", children: [
67
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
68
+ "div",
69
+ {
70
+ className: `editor-tab ${mode === "write" ? "editor-tab--active" : ""}`,
71
+ onClick: () => setMode("write"),
72
+ children: "Write"
73
+ }
74
+ ),
75
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
76
+ "div",
77
+ {
78
+ className: `editor-tab ${mode === "preview" ? "editor-tab--active" : ""}`,
79
+ onClick: () => setMode("preview"),
80
+ children: "Preview"
81
+ }
82
+ )
83
+ ] });
84
+ }
85
+
86
+ // src/components/EditorTextarea.tsx
87
+ var import_react = require("react");
88
+ var import_jsx_runtime3 = require("react/jsx-runtime");
89
+ var EditorTextarea = (0, import_react.forwardRef)(({ value, placeholder, setValue, onImageUpload }, ref) => {
90
+ const handleDrop = async (e) => {
91
+ e.preventDefault();
92
+ if (!onImageUpload) return;
93
+ const files = Array.from(e.dataTransfer.files).filter(
94
+ (file) => file.type.startsWith("image/")
95
+ );
96
+ for (const file of files) {
97
+ const url = await onImageUpload(file);
98
+ setValue(value + `
99
+ ![${file.name}](${url})
100
+ `);
101
+ }
102
+ };
103
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
104
+ "textarea",
105
+ {
106
+ ref,
107
+ value,
108
+ onChange: (e) => setValue(e.target.value),
109
+ onDragOver: (e) => e.preventDefault(),
110
+ onDrop: handleDrop,
111
+ placeholder,
112
+ className: "editor-textarea"
113
+ }
114
+ );
115
+ });
116
+
117
+ // src/components/MarkdownEditor.tsx
118
+ var import_react3 = require("react");
119
+
120
+ // src/hooks/useMarkdownEditor.ts
121
+ var import_react2 = require("react");
122
+ function useMarkdownEditor({ value, setValue }) {
123
+ const textareaRef = (0, import_react2.useRef)(null);
124
+ const insertMarkdown = (0, import_react2.useCallback)(
125
+ (before, after = "") => {
126
+ const textarea = textareaRef.current;
127
+ if (!textarea) return;
128
+ const start = textarea.selectionStart;
129
+ const end = textarea.selectionEnd;
130
+ const selected = value.slice(start, end);
131
+ const replaced = before + (selected || "") + after;
132
+ setValue(value.slice(0, start) + replaced + value.slice(end));
133
+ requestAnimationFrame(() => {
134
+ if (!textareaRef.current) return;
135
+ const next = start + replaced.length;
136
+ textareaRef.current.focus();
137
+ textareaRef.current.selectionStart = next;
138
+ textareaRef.current.selectionEnd = next;
139
+ });
140
+ },
141
+ [value, setValue]
142
+ );
143
+ return {
144
+ textareaRef,
145
+ insertMarkdown
146
+ };
147
+ }
148
+
149
+ // src/components/MarkdownEditor.tsx
150
+ var import_jsx_runtime4 = require("react/jsx-runtime");
151
+ function MarkdownEditor({
152
+ value,
153
+ onChange: setValue,
154
+ onImageUpload
155
+ }) {
156
+ const { textareaRef, insertMarkdown } = useMarkdownEditor({
157
+ value,
158
+ setValue
159
+ });
160
+ const [mode, setMode] = (0, import_react3.useState)("write");
161
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "editor", children: [
162
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
163
+ EditorHeader,
164
+ {
165
+ mode,
166
+ setMode,
167
+ insertMarkdown
168
+ }
169
+ ),
170
+ mode === "write" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
171
+ EditorTextarea,
172
+ {
173
+ ref: textareaRef,
174
+ value,
175
+ setValue,
176
+ onImageUpload
177
+ }
178
+ ) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Preview, { value })
179
+ ] });
180
+ }
181
+
182
+ // src/utils/markdown.ts
183
+ function parseBold(text) {
184
+ return text.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
185
+ }
186
+ function parseItalic(text) {
187
+ return text.replace(/\*(.+?)\*/g, "<em>$1</em>");
188
+ }
189
+ function parseInlineCode(text) {
190
+ return text.replace(/`(.+?)`/g, "<code>$1</code>");
191
+ }
192
+ function parseBlockquote(text) {
193
+ return text.replace(/^> (.+)$/gm, "<blockquote>$1</blockquote>");
194
+ }
195
+ function parseImages(text) {
196
+ return text.replace(
197
+ /!\[(.*?)\]\(([^)]*?)\)/g,
198
+ (_, alt, url) => `<img src="${url}" alt="${alt || ""}" />`
199
+ );
200
+ }
201
+ function parseLinks(text) {
202
+ return text.replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2">$1</a>');
203
+ }
204
+ function parseHeadings(text) {
205
+ return text.replace(/^# (.+)$/gm, "<h1>$1</h1>").replace(/^## (.+)$/gm, "<h2>$1</h2>").replace(/^### (.+)$/gm, "<h3>$1</h3>");
206
+ }
207
+ function parseUnorderedList(text) {
208
+ return text.replace(
209
+ /(^|\r?\n)((?:\s*- .+(?:\r?\n|$))+)/g,
210
+ (_, prefix, block) => {
211
+ const items = block.trim().split(/\r?\n/).map((line) => line.replace(/^\s*- /, "").trim()).filter(Boolean).map((item) => `<li>${item}</li>`).join("");
212
+ return `${prefix}<ul>${items}</ul>`;
213
+ }
214
+ );
215
+ }
216
+ function parseOrderedList(text) {
217
+ return text.replace(
218
+ /(^|\r?\n)((?:\d+\. .+(?:\r?\n|$))+)/g,
219
+ (_, prefix, block) => {
220
+ const items = block.trim().split(/\r?\n/).map((line) => line.replace(/^\d+\. /, "").trim()).filter(Boolean).map((item) => `<li>${item}</li>`).join("");
221
+ return `${prefix}<ol>${items}</ol>`;
222
+ }
223
+ );
224
+ }
225
+ function parseLineBreaks(text) {
226
+ return text.replace(
227
+ /(?<!<\/ul>|<\/ol>|<\/li>|<\/h1>|<\/h2>|<\/h3>)\n/g,
228
+ "<br/>"
229
+ );
230
+ }
231
+ function markdownToHtml(markdown) {
232
+ let html = markdown;
233
+ html = parseBold(html);
234
+ html = parseItalic(html);
235
+ html = parseInlineCode(html);
236
+ html = parseBlockquote(html);
237
+ html = parseImages(html);
238
+ html = parseLinks(html);
239
+ html = parseHeadings(html);
240
+ html = parseUnorderedList(html);
241
+ html = parseOrderedList(html);
242
+ html = parseLineBreaks(html);
243
+ return html;
244
+ }
245
+
246
+ // src/components/Preview.tsx
247
+ var import_jsx_runtime5 = require("react/jsx-runtime");
248
+ function Preview({ value }) {
249
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
250
+ "div",
251
+ {
252
+ className: "preview",
253
+ dangerouslySetInnerHTML: { __html: markdownToHtml(value) },
254
+ style: { listStyle: "decimal" }
255
+ }
256
+ );
257
+ }
258
+
259
+ // src/constants/toolbar.ts
260
+ var import_lucide_react = require("lucide-react");
261
+ var TOOLBAR_BUTTONS = [
262
+ { key: "bold", before: "**", after: "**", Icon: import_lucide_react.BoldIcon },
263
+ { key: "italic", before: "*", after: "*", Icon: import_lucide_react.ItalicIcon },
264
+ { key: "code", before: "`", after: "`", Icon: import_lucide_react.CodeIcon },
265
+ { key: "blockquote", before: "> ", after: "", Icon: import_lucide_react.QuoteIcon },
266
+ { key: "link", before: "[", after: "]()", Icon: import_lucide_react.LinkIcon }
267
+ ];
268
+ var headingOptions = [
269
+ { value: "h1", Icon: import_lucide_react.Heading1Icon, before: "# " },
270
+ { value: "h2", Icon: import_lucide_react.Heading2Icon, before: "## " },
271
+ { value: "h3", Icon: import_lucide_react.Heading3Icon, before: "### " }
272
+ ];
273
+ var listOptions = [
274
+ { value: "unordered list", Icon: import_lucide_react.ListIcon, before: "- " },
275
+ { value: "ordered list", Icon: import_lucide_react.ListOrderedIcon, before: "1. " }
276
+ ];
277
+
278
+ // src/components/Toolbar.tsx
279
+ var import_jsx_runtime6 = require("react/jsx-runtime");
280
+ function Toolbar({ insertMarkdown }) {
281
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("menu", { className: "toolbar-menu", children: [
282
+ TOOLBAR_BUTTONS.map(({ key, before, after, Icon }) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
283
+ "button",
284
+ {
285
+ className: "toolbar-button",
286
+ onClick: () => insertMarkdown(before, after),
287
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Icon, { size: 18 })
288
+ },
289
+ key
290
+ )),
291
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ToolbarDropdownHeading, { insertMarkdown }),
292
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ToolbarDropdownList, { insertMarkdown })
293
+ ] });
294
+ }
295
+
296
+ // src/components/IconDropdown.tsx
297
+ var import_react4 = __toESM(require("react"));
298
+
299
+ // src/utils/cx.ts
300
+ function cx(...classes) {
301
+ return classes.filter(Boolean).join(" ");
302
+ }
303
+
304
+ // src/components/IconDropdown.tsx
305
+ var import_jsx_runtime7 = require("react/jsx-runtime");
306
+ function IconDropdown({
307
+ className,
308
+ disabled,
309
+ options,
310
+ selected,
311
+ triggerIcon,
312
+ onChange
313
+ }) {
314
+ const [isOpen, setIsOpen] = import_react4.default.useState(false);
315
+ const dropdownRef = import_react4.default.useRef(null);
316
+ const toggle = () => {
317
+ if (!disabled) setIsOpen((prev) => !prev);
318
+ };
319
+ import_react4.default.useEffect(() => {
320
+ const handleClickOutside = (e) => {
321
+ if (dropdownRef.current && !dropdownRef.current.contains(e.target)) {
322
+ setIsOpen(false);
323
+ }
324
+ };
325
+ document.addEventListener("mousedown", handleClickOutside);
326
+ return () => document.removeEventListener("mousedown", handleClickOutside);
327
+ }, []);
328
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { ref: dropdownRef, className: cx("md-dropdown", className), children: [
329
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
330
+ "button",
331
+ {
332
+ type: "button",
333
+ onClick: toggle,
334
+ disabled,
335
+ className: cx(
336
+ "md-dropdown__trigger",
337
+ disabled && "md-dropdown__trigger--disabled"
338
+ ),
339
+ children: triggerIcon
340
+ }
341
+ ),
342
+ isOpen && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("ul", { className: "md-dropdown__menu", children: options.map((option) => /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
343
+ "li",
344
+ {
345
+ className: cx(
346
+ "md-dropdown__item",
347
+ selected && "md-dropdown__item--selected"
348
+ ),
349
+ onClick: () => {
350
+ onChange(option.value);
351
+ setIsOpen(false);
352
+ },
353
+ children: [
354
+ option.Icon && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "md-dropdown__icon", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(option.Icon, {}) }),
355
+ option.label && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "md-dropdown__label", children: option.label })
356
+ ]
357
+ },
358
+ option.value
359
+ )) })
360
+ ] });
361
+ }
362
+
363
+ // src/components/ToolbarDropdown.tsx
364
+ var import_lucide_react2 = require("lucide-react");
365
+ var import_jsx_runtime8 = require("react/jsx-runtime");
366
+ function ToolbarDropdownHeading({
367
+ insertMarkdown
368
+ }) {
369
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
370
+ IconDropdown,
371
+ {
372
+ options: headingOptions,
373
+ triggerIcon: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_lucide_react2.Heading1Icon, {}),
374
+ onChange: (value) => {
375
+ const option = headingOptions.find((o) => o.value === value);
376
+ if (option) insertMarkdown(option.before);
377
+ }
378
+ }
379
+ );
380
+ }
381
+ function ToolbarDropdownList({ insertMarkdown }) {
382
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
383
+ IconDropdown,
384
+ {
385
+ options: listOptions,
386
+ triggerIcon: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_lucide_react2.ListIcon, {}),
387
+ onChange: (value) => {
388
+ const option = listOptions.find((o) => o.value === value);
389
+ if (option) insertMarkdown(option.before);
390
+ }
391
+ }
392
+ );
393
+ }
394
+ // Annotate the CommonJS export names for ESM import in node:
395
+ 0 && (module.exports = {
396
+ EditorHeader,
397
+ EditorTab,
398
+ EditorTextarea,
399
+ MarkdownEditor,
400
+ Preview,
401
+ TOOLBAR_BUTTONS,
402
+ Toolbar,
403
+ ToolbarDropdownHeading,
404
+ ToolbarDropdownList,
405
+ cx,
406
+ headingOptions,
407
+ listOptions,
408
+ markdownToHtml,
409
+ useMarkdownEditor
410
+ });