@bendyline/squisq-editor-react 1.2.2 → 1.4.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/EditorContext.d.ts +65 -1
- package/dist/EditorContext.d.ts.map +1 -1
- package/dist/EditorContext.js +31 -4
- package/dist/EditorContext.js.map +1 -1
- package/dist/EditorShell.d.ts +112 -2
- package/dist/EditorShell.d.ts.map +1 -1
- package/dist/EditorShell.js +95 -11
- package/dist/EditorShell.js.map +1 -1
- package/dist/ImageNodeView.d.ts.map +1 -1
- package/dist/ImageNodeView.js +12 -2
- package/dist/ImageNodeView.js.map +1 -1
- package/dist/MediaBin.d.ts +12 -1
- package/dist/MediaBin.d.ts.map +1 -1
- package/dist/MediaBin.js +29 -4
- package/dist/MediaBin.js.map +1 -1
- package/dist/MentionExtension.d.ts +22 -0
- package/dist/MentionExtension.d.ts.map +1 -0
- package/dist/MentionExtension.js +242 -0
- package/dist/MentionExtension.js.map +1 -0
- package/dist/RawEditor.d.ts +8 -1
- package/dist/RawEditor.d.ts.map +1 -1
- package/dist/RawEditor.js +167 -30
- package/dist/RawEditor.js.map +1 -1
- package/dist/TemplateAnnotation.d.ts.map +1 -1
- package/dist/TemplateAnnotation.js +4 -2
- package/dist/TemplateAnnotation.js.map +1 -1
- package/dist/Toolbar.d.ts +7 -1
- package/dist/Toolbar.d.ts.map +1 -1
- package/dist/Toolbar.js +57 -18
- package/dist/Toolbar.js.map +1 -1
- package/dist/Tooltip.d.ts +10 -0
- package/dist/Tooltip.d.ts.map +1 -0
- package/dist/Tooltip.js +104 -0
- package/dist/Tooltip.js.map +1 -0
- package/dist/ViewSwitcher.d.ts +1 -1
- package/dist/ViewSwitcher.d.ts.map +1 -1
- package/dist/ViewSwitcher.js +10 -4
- package/dist/ViewSwitcher.js.map +1 -1
- package/dist/WysiwygEditor.d.ts +13 -2
- package/dist/WysiwygEditor.d.ts.map +1 -1
- package/dist/WysiwygEditor.js +239 -4
- package/dist/WysiwygEditor.js.map +1 -1
- package/dist/__tests__/detectMarkdown.test.d.ts +2 -0
- package/dist/__tests__/detectMarkdown.test.d.ts.map +1 -0
- package/dist/__tests__/detectMarkdown.test.js +69 -0
- package/dist/__tests__/detectMarkdown.test.js.map +1 -0
- package/dist/__tests__/fileKind.test.d.ts +2 -0
- package/dist/__tests__/fileKind.test.d.ts.map +1 -0
- package/dist/__tests__/fileKind.test.js +81 -0
- package/dist/__tests__/fileKind.test.js.map +1 -0
- package/dist/__tests__/mediaAttachmentFlow.test.d.ts +2 -0
- package/dist/__tests__/mediaAttachmentFlow.test.d.ts.map +1 -0
- package/dist/__tests__/mediaAttachmentFlow.test.js +99 -0
- package/dist/__tests__/mediaAttachmentFlow.test.js.map +1 -0
- package/dist/__tests__/tiptapBridge.test.js +49 -0
- package/dist/__tests__/tiptapBridge.test.js.map +1 -1
- package/dist/__tests__/tiptapImageRoundTrip.test.d.ts +2 -0
- package/dist/__tests__/tiptapImageRoundTrip.test.d.ts.map +1 -0
- package/dist/__tests__/tiptapImageRoundTrip.test.js +68 -0
- package/dist/__tests__/tiptapImageRoundTrip.test.js.map +1 -0
- package/dist/detectMarkdown.d.ts +20 -0
- package/dist/detectMarkdown.d.ts.map +1 -0
- package/dist/detectMarkdown.js +61 -0
- package/dist/detectMarkdown.js.map +1 -0
- package/dist/fileKind.d.ts +30 -0
- package/dist/fileKind.d.ts.map +1 -0
- package/dist/fileKind.js +123 -0
- package/dist/fileKind.js.map +1 -0
- package/dist/hooks/useFileDrop.d.ts.map +1 -1
- package/dist/hooks/useFileDrop.js +9 -7
- package/dist/hooks/useFileDrop.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/mediaDragMime.d.ts +17 -0
- package/dist/mediaDragMime.d.ts.map +1 -0
- package/dist/mediaDragMime.js +22 -0
- package/dist/mediaDragMime.js.map +1 -0
- package/dist/tiptapBridge.d.ts.map +1 -1
- package/dist/tiptapBridge.js +99 -6
- package/dist/tiptapBridge.js.map +1 -1
- package/package.json +9 -7
- package/src/EditorContext.tsx +106 -3
- package/src/EditorShell.tsx +313 -21
- package/src/ImageNodeView.tsx +15 -2
- package/src/MediaBin.tsx +45 -4
- package/src/MentionExtension.tsx +258 -0
- package/src/RawEditor.tsx +193 -37
- package/src/TemplateAnnotation.ts +4 -2
- package/src/Toolbar.tsx +111 -48
- package/src/Tooltip.tsx +124 -0
- package/src/ViewSwitcher.tsx +15 -5
- package/src/WysiwygEditor.tsx +270 -5
- package/src/__tests__/detectMarkdown.test.ts +88 -0
- package/src/__tests__/fileKind.test.ts +96 -0
- package/src/__tests__/mediaAttachmentFlow.test.ts +110 -0
- package/src/__tests__/tiptapBridge.test.ts +58 -0
- package/src/__tests__/tiptapImageRoundTrip.test.ts +73 -0
- package/src/detectMarkdown.ts +62 -0
- package/src/fileKind.ts +134 -0
- package/src/hooks/useFileDrop.ts +10 -6
- package/src/index.ts +11 -0
- package/src/mediaDragMime.ts +32 -0
- package/src/styles/editor.css +214 -8
- package/src/tiptapBridge.ts +107 -6
|
@@ -49,6 +49,31 @@ describe('markdownToTiptap', () => {
|
|
|
49
49
|
expect(html).toContain('Example');
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
+
it('converts mentions to chip spans', () => {
|
|
53
|
+
const html = markdownToTiptap('Hey @[Leo](gezel:leo), take a look.');
|
|
54
|
+
expect(html).toContain('data-mention="true"');
|
|
55
|
+
expect(html).toContain('data-kind="gezel"');
|
|
56
|
+
expect(html).toContain('data-id="leo"');
|
|
57
|
+
expect(html).toContain('data-label="Leo"');
|
|
58
|
+
// "@Leo" appears inside the chip — NOT as a broken link
|
|
59
|
+
expect(html).not.toContain('href="gezel:leo"');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('tolerates the backslash-escaped colon remark emits', () => {
|
|
63
|
+
// remark-stringify sometimes emits `gezel\:leo` to disambiguate
|
|
64
|
+
// from autolink syntax. The bridge should still recognize it.
|
|
65
|
+
const html = markdownToTiptap('Hey @[Leo](gezel\\:leo).');
|
|
66
|
+
expect(html).toContain('data-kind="gezel"');
|
|
67
|
+
expect(html).toContain('data-id="leo"');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('round-trips mentions back to markdown', () => {
|
|
71
|
+
const md = 'Hey @[Leo](gezel:leo), ping @[Tess](gezel:tess) too.';
|
|
72
|
+
const html = markdownToTiptap(md);
|
|
73
|
+
const back = tiptapToMarkdown(html);
|
|
74
|
+
expect(back.trim()).toBe(md);
|
|
75
|
+
});
|
|
76
|
+
|
|
52
77
|
it('converts images', () => {
|
|
53
78
|
const html = markdownToTiptap('');
|
|
54
79
|
expect(html).toContain('alt="Logo"');
|
|
@@ -165,6 +190,20 @@ describe('tiptapToMarkdown', () => {
|
|
|
165
190
|
expect(md).toContain('`foo`');
|
|
166
191
|
});
|
|
167
192
|
|
|
193
|
+
it('converts images regardless of attribute order', () => {
|
|
194
|
+
// TipTap's default Image extension renders `src` before `alt`. The
|
|
195
|
+
// tiptapToMarkdown parser previously required `alt` first and
|
|
196
|
+
// silently dropped every src-first image, so a pasted/toolbar
|
|
197
|
+
// image attached in the WYSIWYG editor never made it back into
|
|
198
|
+
// the outgoing markdown.
|
|
199
|
+
const altFirst = tiptapToMarkdown('<p><img alt="Logo" src="logo.png"></p>');
|
|
200
|
+
expect(altFirst).toContain('');
|
|
201
|
+
const srcFirst = tiptapToMarkdown('<p><img src="attachments/xyz.png" alt="xyz"></p>');
|
|
202
|
+
expect(srcFirst).toContain('');
|
|
203
|
+
const srcOnly = tiptapToMarkdown('<p><img src="attachments/nm.png"></p>');
|
|
204
|
+
expect(srcOnly).toContain('');
|
|
205
|
+
});
|
|
206
|
+
|
|
168
207
|
it('converts links', () => {
|
|
169
208
|
const md = tiptapToMarkdown('<p><a href="https://example.com">Example</a></p>');
|
|
170
209
|
expect(md).toContain('[Example](https://example.com)');
|
|
@@ -230,6 +269,25 @@ describe('tiptapToMarkdown', () => {
|
|
|
230
269
|
expect(md).toContain('&');
|
|
231
270
|
expect(md).toContain('"test"');
|
|
232
271
|
});
|
|
272
|
+
|
|
273
|
+
it('converts <br> to a hard line break (two trailing spaces)', () => {
|
|
274
|
+
const md = tiptapToMarkdown('<p>line one<br>line two</p>');
|
|
275
|
+
expect(md).toContain('line one \nline two');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('preserves paragraph break inside list items', () => {
|
|
279
|
+
const md = tiptapToMarkdown(
|
|
280
|
+
'<ul><li><p><strong>Title</strong></p><p>Description text</p></li></ul>',
|
|
281
|
+
);
|
|
282
|
+
expect(md).toContain('- **Title**');
|
|
283
|
+
expect(md).toContain(' Description text');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('preserves <br> hard break inside list items', () => {
|
|
287
|
+
const md = tiptapToMarkdown('<ul><li><p>First line<br>Second line</p></li></ul>');
|
|
288
|
+
expect(md).toContain('- First line ');
|
|
289
|
+
expect(md).toContain(' Second line');
|
|
290
|
+
});
|
|
233
291
|
});
|
|
234
292
|
|
|
235
293
|
// ---------------------------------------------------------------------------
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Editor } from '@tiptap/core';
|
|
2
|
+
import Image from '@tiptap/extension-image';
|
|
3
|
+
import Document from '@tiptap/extension-document';
|
|
4
|
+
import Paragraph from '@tiptap/extension-paragraph';
|
|
5
|
+
import Text from '@tiptap/extension-text';
|
|
6
|
+
import { describe, expect, it } from 'vitest';
|
|
7
|
+
import { tiptapToMarkdown } from '../tiptapBridge';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The actual end-to-end question the chat composer hinges on:
|
|
11
|
+
*
|
|
12
|
+
* when a pasted / uploaded image is inserted into the tiptap
|
|
13
|
+
* editor via `setImage({src, alt})`, does the markdown we serialize
|
|
14
|
+
* out (via `getHTML()` + `tiptapToMarkdown`) contain
|
|
15
|
+
* `` — the shape the gezel service's image-attachment
|
|
16
|
+
* extractor expects?
|
|
17
|
+
*
|
|
18
|
+
* Unit tests on the regex alone have been green the whole time, and
|
|
19
|
+
* yet the production app kept sending image-less messages. This test
|
|
20
|
+
* exercises the real tiptap editor in jsdom so the HTML tiptap
|
|
21
|
+
* actually emits is what we check, not a hand-synthesized string.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
function makeEditor() {
|
|
25
|
+
return new Editor({
|
|
26
|
+
// Bare-minimum schema: doc → paragraph (text) + block image. No
|
|
27
|
+
// React node-view — we only care about the serialized HTML.
|
|
28
|
+
extensions: [Document, Paragraph, Text, Image.configure({ inline: false })],
|
|
29
|
+
content: '<p></p>',
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('tiptap Image node → markdown round-trip', () => {
|
|
34
|
+
it('produces `` for an image inserted via setImage', () => {
|
|
35
|
+
const editor = makeEditor();
|
|
36
|
+
editor.chain().focus().setImage({ src: 'attachments/xyz.png', alt: 'my screenshot' }).run();
|
|
37
|
+
const html = editor.getHTML();
|
|
38
|
+
const md = tiptapToMarkdown(html);
|
|
39
|
+
editor.destroy();
|
|
40
|
+
expect(html).toMatch(/<img\b/);
|
|
41
|
+
expect(html).toContain('src="attachments/xyz.png"');
|
|
42
|
+
expect(md).toContain('');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('produces `` when alt is empty (most common pasted-image shape)', () => {
|
|
46
|
+
const editor = makeEditor();
|
|
47
|
+
editor.chain().focus().setImage({ src: 'attachments/pasted.png', alt: '' }).run();
|
|
48
|
+
const md = tiptapToMarkdown(editor.getHTML());
|
|
49
|
+
editor.destroy();
|
|
50
|
+
expect(md).toContain('');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('produces `` when alt is omitted entirely', () => {
|
|
54
|
+
const editor = makeEditor();
|
|
55
|
+
editor.chain().focus().setImage({ src: 'attachments/no-alt.png' }).run();
|
|
56
|
+
const md = tiptapToMarkdown(editor.getHTML());
|
|
57
|
+
editor.destroy();
|
|
58
|
+
expect(md).toContain('');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('coexists with paragraph text', () => {
|
|
62
|
+
// Seed the editor with a paragraph, then insert the image after
|
|
63
|
+
// it — mirrors the common case where the user types "here:" and
|
|
64
|
+
// then pastes an image.
|
|
65
|
+
const editor = makeEditor();
|
|
66
|
+
editor.chain().focus().insertContent('here you go').run();
|
|
67
|
+
editor.chain().focus().setImage({ src: 'attachments/foo.png', alt: 'foo' }).run();
|
|
68
|
+
const md = tiptapToMarkdown(editor.getHTML());
|
|
69
|
+
editor.destroy();
|
|
70
|
+
expect(md).toContain('');
|
|
71
|
+
expect(md).toContain('here you go');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown detection — heuristic for spotting markdown source in pasted text.
|
|
3
|
+
*
|
|
4
|
+
* Used by the WYSIWYG editor's paste handler to decide whether plain-text
|
|
5
|
+
* paste content should be parsed as markdown rather than inserted literally.
|
|
6
|
+
*
|
|
7
|
+
* The heuristic prefers false negatives (treating markdown as plain text)
|
|
8
|
+
* over false positives (mangling plain text that happens to contain a few
|
|
9
|
+
* special characters).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Block-level patterns that strongly indicate markdown. Any one is enough. */
|
|
13
|
+
const STRONG_BLOCK_PATTERNS: RegExp[] = [
|
|
14
|
+
/^#{1,6}\s+\S/, // # Heading
|
|
15
|
+
/^[-*+]\s+\S/, // - bullet
|
|
16
|
+
/^\d+\.\s+\S/, // 1. ordered
|
|
17
|
+
/^>\s+\S/, // > blockquote
|
|
18
|
+
/^```/, // ``` code fence
|
|
19
|
+
/^\|.+\|\s*$/, // | table | row |
|
|
20
|
+
/^[-*+]\s+\[[ xX]\]\s+/, // - [ ] task
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/** Inline patterns that are weaker indicators on their own. */
|
|
24
|
+
const INLINE_PATTERNS: RegExp[] = [
|
|
25
|
+
/\*\*[^*\n]+\*\*/, // **bold**
|
|
26
|
+
/__[^_\n]+__/, // __bold__
|
|
27
|
+
/`[^`\n]+`/, // `code`
|
|
28
|
+
/\[[^\]\n]+\]\([^)\n]+\)/, // [link](url)
|
|
29
|
+
/!\[[^\]\n]*\]\([^)\n]+\)/, // 
|
|
30
|
+
/~~[^~\n]+~~/, // ~~strike~~
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns true if the text looks like markdown source.
|
|
35
|
+
*
|
|
36
|
+
* Detection rules:
|
|
37
|
+
* - Any line matching a strong block pattern → yes
|
|
38
|
+
* - Two or more inline pattern matches anywhere in the text → yes
|
|
39
|
+
* - Otherwise → no
|
|
40
|
+
*/
|
|
41
|
+
export function looksLikeMarkdown(text: string): boolean {
|
|
42
|
+
if (!text || text.length < 2) return false;
|
|
43
|
+
|
|
44
|
+
const lines = text.split(/\r?\n/);
|
|
45
|
+
for (const line of lines) {
|
|
46
|
+
const trimmed = line.trim();
|
|
47
|
+
if (!trimmed) continue;
|
|
48
|
+
for (const re of STRONG_BLOCK_PATTERNS) {
|
|
49
|
+
if (re.test(trimmed)) return true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let inlineHits = 0;
|
|
54
|
+
for (const re of INLINE_PATTERNS) {
|
|
55
|
+
if (re.test(text)) {
|
|
56
|
+
inlineHits++;
|
|
57
|
+
if (inlineHits >= 2) return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return false;
|
|
62
|
+
}
|
package/src/fileKind.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fileKind
|
|
3
|
+
*
|
|
4
|
+
* Maps a file name (or bare extension) to a Monaco language ID and decides
|
|
5
|
+
* whether the editor shell should operate in markdown mode (full WYSIWYG +
|
|
6
|
+
* Preview experience) or code mode (Monaco-only view with formatting
|
|
7
|
+
* buttons hidden).
|
|
8
|
+
*
|
|
9
|
+
* The mapping favors common web / systems languages that Monaco ships with
|
|
10
|
+
* out of the box. Unknown extensions fall back to markdown mode so the
|
|
11
|
+
* existing UX remains the default for anything we don't recognize.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export interface FileKind {
|
|
15
|
+
/** 'markdown' keeps the full editor (WYSIWYG + Preview tabs); 'code' is Monaco-only. */
|
|
16
|
+
mode: 'markdown' | 'code';
|
|
17
|
+
/** Monaco language ID — passed to `<Editor defaultLanguage={...} />`. */
|
|
18
|
+
language: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Extension → Monaco language ID. Keys are lowercase, no leading dot.
|
|
23
|
+
* Extend as needed; unknown extensions fall back to markdown mode.
|
|
24
|
+
*/
|
|
25
|
+
const EXT_TO_LANGUAGE: Record<string, string> = {
|
|
26
|
+
md: 'markdown',
|
|
27
|
+
markdown: 'markdown',
|
|
28
|
+
mdown: 'markdown',
|
|
29
|
+
txt: 'plaintext',
|
|
30
|
+
ts: 'typescript',
|
|
31
|
+
tsx: 'typescript',
|
|
32
|
+
js: 'javascript',
|
|
33
|
+
jsx: 'javascript',
|
|
34
|
+
mjs: 'javascript',
|
|
35
|
+
cjs: 'javascript',
|
|
36
|
+
json: 'json',
|
|
37
|
+
jsonc: 'json',
|
|
38
|
+
html: 'html',
|
|
39
|
+
htm: 'html',
|
|
40
|
+
css: 'css',
|
|
41
|
+
scss: 'scss',
|
|
42
|
+
less: 'less',
|
|
43
|
+
py: 'python',
|
|
44
|
+
go: 'go',
|
|
45
|
+
rs: 'rust',
|
|
46
|
+
rb: 'ruby',
|
|
47
|
+
java: 'java',
|
|
48
|
+
c: 'c',
|
|
49
|
+
h: 'c',
|
|
50
|
+
cpp: 'cpp',
|
|
51
|
+
hpp: 'cpp',
|
|
52
|
+
cc: 'cpp',
|
|
53
|
+
cs: 'csharp',
|
|
54
|
+
php: 'php',
|
|
55
|
+
sh: 'shell',
|
|
56
|
+
bash: 'shell',
|
|
57
|
+
zsh: 'shell',
|
|
58
|
+
yml: 'yaml',
|
|
59
|
+
yaml: 'yaml',
|
|
60
|
+
toml: 'ini',
|
|
61
|
+
ini: 'ini',
|
|
62
|
+
xml: 'xml',
|
|
63
|
+
svg: 'xml',
|
|
64
|
+
sql: 'sql',
|
|
65
|
+
lua: 'lua',
|
|
66
|
+
swift: 'swift',
|
|
67
|
+
kt: 'kotlin',
|
|
68
|
+
kts: 'kotlin',
|
|
69
|
+
dockerfile: 'dockerfile',
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Languages that keep the full markdown shell (WYSIWYG + Preview). Anything
|
|
74
|
+
* outside this set is treated as code.
|
|
75
|
+
*/
|
|
76
|
+
const MARKDOWN_MODE_LANGUAGES = new Set(['markdown', 'plaintext']);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Pull the lowercase extension (no leading dot) from a file name or bare
|
|
80
|
+
* extension string. Returns null when none is discernible.
|
|
81
|
+
*
|
|
82
|
+
* Examples:
|
|
83
|
+
* "foo.ts" → "ts"
|
|
84
|
+
* "foo.tar.gz" → "gz"
|
|
85
|
+
* ".ts" → "ts"
|
|
86
|
+
* "ts" → "ts"
|
|
87
|
+
* "Dockerfile" → "dockerfile" (full name match for extensionless files)
|
|
88
|
+
* "" → null
|
|
89
|
+
*/
|
|
90
|
+
function extractExtension(fileName: string): string | null {
|
|
91
|
+
const trimmed = fileName.trim();
|
|
92
|
+
if (!trimmed) return null;
|
|
93
|
+
|
|
94
|
+
// Strip any leading path — take only the basename.
|
|
95
|
+
const base = trimmed.replace(/^.*[/\\]/, '');
|
|
96
|
+
if (!base) return null;
|
|
97
|
+
|
|
98
|
+
const dotIdx = base.lastIndexOf('.');
|
|
99
|
+
if (dotIdx === -1) {
|
|
100
|
+
// No dot — could still be a recognized bare name (Dockerfile) or a bare
|
|
101
|
+
// extension passed by a caller like "ts". Lower-case and return.
|
|
102
|
+
return base.toLowerCase();
|
|
103
|
+
}
|
|
104
|
+
if (dotIdx === base.length - 1) return null; // Trailing dot, no extension.
|
|
105
|
+
return base.slice(dotIdx + 1).toLowerCase();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Detect a Monaco language ID from a file name. Returns null when the
|
|
110
|
+
* extension (or bare name) is not in the mapping.
|
|
111
|
+
*/
|
|
112
|
+
export function detectLanguageFromFileName(fileName: string): string | null {
|
|
113
|
+
const ext = extractExtension(fileName);
|
|
114
|
+
if (!ext) return null;
|
|
115
|
+
return EXT_TO_LANGUAGE[ext] ?? null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Resolve the editor mode + Monaco language for a given file. The explicit
|
|
120
|
+
* `language` argument, if provided, wins over any detection from
|
|
121
|
+
* `fileName`. When nothing matches, falls back to markdown mode.
|
|
122
|
+
*/
|
|
123
|
+
export function resolveFileKind(fileName?: string, language?: string): FileKind {
|
|
124
|
+
const resolvedLanguage = language ?? (fileName ? detectLanguageFromFileName(fileName) : null);
|
|
125
|
+
|
|
126
|
+
if (!resolvedLanguage) {
|
|
127
|
+
return { mode: 'markdown', language: 'markdown' };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const mode: FileKind['mode'] = MARKDOWN_MODE_LANGUAGES.has(resolvedLanguage)
|
|
131
|
+
? 'markdown'
|
|
132
|
+
: 'code';
|
|
133
|
+
return { mode, language: resolvedLanguage };
|
|
134
|
+
}
|
package/src/hooks/useFileDrop.ts
CHANGED
|
@@ -137,17 +137,21 @@ export function useFileDrop({ onDrop, enabled = true }: UseFileDropOptions): Use
|
|
|
137
137
|
const handleDragEnter = useCallback(
|
|
138
138
|
(e: React.DragEvent) => {
|
|
139
139
|
if (!enabled) return;
|
|
140
|
+
|
|
141
|
+
// Only react to OS file drags. In-app drags (e.g. dragging a thumbnail
|
|
142
|
+
// out of the MediaBin) don't carry file-kind items and must pass
|
|
143
|
+
// through to the editors without showing the drop overlay.
|
|
144
|
+
const classification = e.dataTransfer.items
|
|
145
|
+
? classifyDataTransferItems(e.dataTransfer.items)
|
|
146
|
+
: 'mixed';
|
|
147
|
+
if (!classification) return;
|
|
148
|
+
|
|
140
149
|
e.preventDefault();
|
|
141
150
|
dragCounterRef.current++;
|
|
142
151
|
|
|
143
152
|
if (dragCounterRef.current === 1) {
|
|
144
153
|
setIsDragging(true);
|
|
145
|
-
|
|
146
|
-
setDragContentType(classifyDataTransferItems(e.dataTransfer.items));
|
|
147
|
-
} else {
|
|
148
|
-
// Fallback: can't classify, show all zones
|
|
149
|
-
setDragContentType('mixed');
|
|
150
|
-
}
|
|
154
|
+
setDragContentType(classification);
|
|
151
155
|
}
|
|
152
156
|
},
|
|
153
157
|
[enabled],
|
package/src/index.ts
CHANGED
|
@@ -25,12 +25,21 @@ export type { EditorShellProps, EditorTheme } from './EditorShell.js';
|
|
|
25
25
|
export { EditorProvider, useEditorContext } from './EditorContext.js';
|
|
26
26
|
export type {
|
|
27
27
|
EditorView,
|
|
28
|
+
EditorMode,
|
|
28
29
|
EditorState,
|
|
29
30
|
EditorActions,
|
|
30
31
|
EditorContextValue,
|
|
31
32
|
EditorProviderProps,
|
|
33
|
+
ImageDisplayMode,
|
|
34
|
+
MentionCandidate,
|
|
35
|
+
MentionProvider,
|
|
32
36
|
} from './EditorContext.js';
|
|
33
37
|
|
|
38
|
+
// File-kind detection — useful for hosts that want to pre-decide chrome
|
|
39
|
+
// around the editor based on whether a file is markdown or code.
|
|
40
|
+
export { resolveFileKind, detectLanguageFromFileName } from './fileKind.js';
|
|
41
|
+
export type { FileKind } from './fileKind.js';
|
|
42
|
+
|
|
34
43
|
// Individual editors (for custom layouts)
|
|
35
44
|
export { RawEditor } from './RawEditor.js';
|
|
36
45
|
export type { RawEditorProps } from './RawEditor.js';
|
|
@@ -60,6 +69,8 @@ export type { MediaBinProps } from './MediaBin.js';
|
|
|
60
69
|
export { StatusBar } from './StatusBar.js';
|
|
61
70
|
export type { StatusBarProps } from './StatusBar.js';
|
|
62
71
|
|
|
72
|
+
export { TooltipLayer } from './Tooltip.js';
|
|
73
|
+
|
|
63
74
|
// Drag-and-drop
|
|
64
75
|
export { DropZoneOverlay } from './DropZoneOverlay.js';
|
|
65
76
|
export type { DropZoneOverlayProps } from './DropZoneOverlay.js';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared MIME type used to signal an in-app drag from the MediaBin to either
|
|
3
|
+
* the Raw or WYSIWYG editor. Carries a JSON payload of the form
|
|
4
|
+
* `{ name, mimeType, alt }` so the receiving editor can insert a reference
|
|
5
|
+
* to an existing media entry without re-uploading it.
|
|
6
|
+
*/
|
|
7
|
+
export const SQUISQ_MEDIA_MIME = 'application/x-squisq-media';
|
|
8
|
+
|
|
9
|
+
export interface SquisqMediaDragPayload {
|
|
10
|
+
/** Relative path / filename as stored in the MediaProvider. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** MIME type of the entry. */
|
|
13
|
+
mimeType: string;
|
|
14
|
+
/** Default alt text derived from the filename. */
|
|
15
|
+
alt: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function parseSquisqMediaPayload(raw: string): SquisqMediaDragPayload | null {
|
|
19
|
+
try {
|
|
20
|
+
const parsed = JSON.parse(raw) as Partial<SquisqMediaDragPayload>;
|
|
21
|
+
if (
|
|
22
|
+
typeof parsed.name === 'string' &&
|
|
23
|
+
typeof parsed.mimeType === 'string' &&
|
|
24
|
+
typeof parsed.alt === 'string'
|
|
25
|
+
) {
|
|
26
|
+
return parsed as SquisqMediaDragPayload;
|
|
27
|
+
}
|
|
28
|
+
} catch {
|
|
29
|
+
// fall through
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|