markdown-text-editor 0.0.6-beta.1 → 0.0.8-beta.1

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/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "markdown-text-editor",
3
- "version": "0.0.6-beta.1",
3
+ "version": "0.0.8-beta.1",
4
4
  "description": "A powerful, easy-to-use Markdown editor with a real-time preview, syntax highlighting. Ideal for developers, writers, and content creators who need a seamless, interactive writing experience with full Markdown support.",
5
- "main": "dist/markdown-text-editor.min.js",
5
+ "main": "dist/markdown-text-editor.js",
6
6
  "repository": {
7
7
  "type": "github",
8
8
  "url": "git+https://github.com/nezanuha/markdown-text-editor.git"
@@ -18,6 +18,7 @@
18
18
  "markdown-text-editor",
19
19
  "markdown-text",
20
20
  "mde",
21
+ "text-editor",
21
22
  "simple mde",
22
23
  "markdown",
23
24
  "editor",
@@ -41,7 +42,10 @@
41
42
  "url": "https://github.com/nezanuha/markdown-text-editor/issues"
42
43
  },
43
44
  "files": [
44
- "dist/**/*"
45
+ "dist",
46
+ "src",
47
+ "README.md",
48
+ "LICENSE"
45
49
  ],
46
50
  "dependencies": {
47
51
  "marked": "^15.0.4",
@@ -0,0 +1,2 @@
1
+ // main.js
2
+ export { default as markdownEditor } from './markdown/editor';
@@ -0,0 +1,36 @@
1
+ // #markdown\Toolbar\index.js
2
+ import BoldTool from './tools/BoldTool.js';
3
+ import ItalicTool from './tools/ItalicTool.js';
4
+ import StrikethroughTool from './tools/StrikethroughTool.js';
5
+ import PreviewTool from './tools/PreviewTool.js'
6
+
7
+ class Toolbar {
8
+ constructor(editor, options) {
9
+ this.editor = editor;
10
+ this.options = options;
11
+ this.toolbar = document.createElement('div');
12
+ this.toolbar.className = 'toolbar flex space-x-1.5 p-1.5 bg-stone-100 dark:bg-stone-900 dark:text-stone-200 border-b border-stone-200 dark:border-stone-700';
13
+ this.init();
14
+ }
15
+
16
+ init() {
17
+ const toolMapping = {
18
+ bold: BoldTool,
19
+ italic: ItalicTool,
20
+ strikethrough: StrikethroughTool,
21
+ preview: PreviewTool
22
+ };
23
+
24
+ this.options.forEach(tool => {
25
+ const ToolClass = toolMapping[tool];
26
+ if (ToolClass) {
27
+ const toolInstance = new ToolClass(this.editor);
28
+ this.toolbar.appendChild(toolInstance.button);
29
+ }
30
+ });
31
+
32
+ this.editor.editorContainer.insertBefore(this.toolbar, this.editor.markdownEditorDiv);
33
+ }
34
+ }
35
+
36
+ export default Toolbar;
@@ -0,0 +1,15 @@
1
+ import MarkdownTool from './MarkdownTool.js';
2
+
3
+ class BoldTool extends MarkdownTool {
4
+ constructor(editor) {
5
+ // Call the parent constructor with the markdown syntax for bold (**)
6
+ super(editor, '**', 'bold text');
7
+ this.button = this.createButton(`
8
+ <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
9
+ <path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/>
10
+ </svg>
11
+ `);
12
+ }
13
+ }
14
+
15
+ export default BoldTool;
@@ -0,0 +1,17 @@
1
+ import MarkdownTool from './MarkdownTool.js';
2
+
3
+ class ItalicTool extends MarkdownTool {
4
+ constructor(editor) {
5
+ // Call the parent constructor with the markdown syntax for italic (*)
6
+ super(editor, '*', 'italic text');
7
+ this.button = this.createButton(`
8
+ <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
9
+ <line x1="19" y1="4" x2="10" y2="4"/>
10
+ <line x1="14" y1="20" x2="5" y2="20"/>
11
+ <line x1="15" y1="4" x2="9" y2="20"/>
12
+ </svg>
13
+ `);
14
+ }
15
+ }
16
+
17
+ export default ItalicTool;
@@ -0,0 +1,39 @@
1
+ // #components\Toolbar\tools\MarkdownTool.js
2
+ class MarkdownTool {
3
+ constructor(editor, syntax, defaultText) {
4
+ this.editor = editor;
5
+ this.syntax = syntax; // Markdown syntax (e.g., ** for bold, * for italic)
6
+ this.defaultText = defaultText; // Default text if nothing is selected
7
+ this.button = this.createButton();
8
+ }
9
+
10
+ // Create a button element (can be overridden in child classes)
11
+ createButton(iconHtml) {
12
+ const button = document.createElement('button');
13
+ button.innerHTML = iconHtml; // Pass icon HTML from child classes
14
+ button.className = 'markdown-btn p-2 hover:bg-stone-200 dark:hover:bg-stone-600 rounded duration-300';
15
+ button.addEventListener('click', () => this.applySyntax());
16
+ return button;
17
+ }
18
+
19
+ // Toggle markdown syntax at the current cursor position
20
+ applySyntax() {
21
+ const textarea = this.editor.usertextarea;
22
+ const { selectionStart, selectionEnd } = textarea;
23
+ const selectedText = textarea.value.substring(selectionStart, selectionEnd);
24
+
25
+ const syntaxLength = this.syntax.length;
26
+
27
+ if (selectedText.startsWith(this.syntax) && selectedText.endsWith(this.syntax)) {
28
+ // If text is already wrapped with the markdown syntax, remove it
29
+ const unformattedText = selectedText.slice(syntaxLength, -syntaxLength);
30
+ this.editor.insertText(unformattedText);
31
+ } else {
32
+ // If no text is selected or it's not wrapped, add the markdown syntax
33
+ const newText = `${this.syntax}${selectedText || this.defaultText}${this.syntax}`;
34
+ this.editor.insertText(newText);
35
+ }
36
+ }
37
+ }
38
+
39
+ export default MarkdownTool;
@@ -0,0 +1,92 @@
1
+ // #components/Toolbar/tools/PreviewToggleTool.js
2
+ import MarkdownTool from './MarkdownTool.js';
3
+
4
+ class PreviewTool extends MarkdownTool {
5
+ constructor(editor) {
6
+ // No markdown syntax for preview toggle, so we call the parent constructor with empty values
7
+ super(editor, '', 'Preview');
8
+ this.button = this.createButton(`
9
+ <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
10
+ <rect width="18" height="18" x="3" y="3" rx="2"/><path d="M12 3v18"/>
11
+ </svg>
12
+ `);
13
+ }
14
+
15
+ // Override applySyntax to handle preview toggling
16
+ applySyntax() {
17
+ const previewWrapper = this.editor.previewContent?.parentNode;
18
+ const editorDiv = this.editor.markdownEditorDiv;
19
+
20
+ if (!previewWrapper || !editorDiv) return;
21
+
22
+ // Toggle the preview's visibility by switching between 'block' and 'hidden' classes
23
+ if (this.editor.preview) {
24
+ this.enablePreview(previewWrapper, editorDiv);
25
+ } else {
26
+ this.disablePreview(previewWrapper, editorDiv);
27
+ }
28
+ }
29
+
30
+ // Method to hide the preview (disable it)
31
+ disablePreview(previewWrapper, editorDiv) {
32
+
33
+ this.editor.preview = true;
34
+
35
+ editorDiv.parentNode.classList.toggle('fixed');
36
+ editorDiv.parentNode.classList.toggle('top-0');
37
+ editorDiv.parentNode.classList.toggle('inset-x-0');
38
+ editorDiv.parentNode.classList.toggle('rounded-md');
39
+
40
+ previewWrapper.classList.toggle('hidden');
41
+
42
+ // Add grid layout and divide classes to the editor div
43
+ editorDiv.classList.remove(
44
+ 'md:grid',
45
+ 'md:grid-cols-2',
46
+ 'md:divide-x',
47
+ 'md:divide-stone-300',
48
+ 'dark:md:divide-stone-700'
49
+ );
50
+
51
+ editorDiv.querySelector(".textarea-wrapper").classList.remove(
52
+ 'h-lvh',
53
+ 'hidden',
54
+ 'md:block'
55
+ );
56
+ this.editor.render(); // Re-render content in the preview
57
+
58
+ document.querySelector("body").classList.remove('overflow-hidden');
59
+ }
60
+
61
+ // Method to show the preview (enable it)
62
+ enablePreview(previewWrapper, editorDiv) {
63
+
64
+ this.editor.preview = false;
65
+
66
+ editorDiv.parentNode.classList.toggle('fixed');
67
+ editorDiv.parentNode.classList.toggle('top-0');
68
+ editorDiv.parentNode.classList.toggle('inset-x-0');
69
+ editorDiv.parentNode.classList.toggle('rounded-md');
70
+
71
+ previewWrapper.classList.toggle('hidden');
72
+ // Remove grid layout and divide classes from the editor div
73
+ editorDiv.classList.add(
74
+ 'md:grid',
75
+ 'md:grid-cols-2',
76
+ 'md:divide-x',
77
+ 'md:divide-stone-300',
78
+ 'dark:md:divide-stone-700'
79
+ );
80
+
81
+ editorDiv.querySelector(".textarea-wrapper").classList.add(
82
+ 'h-lvh',
83
+ 'hidden',
84
+ 'md:block'
85
+ );
86
+ // editorDiv.closest(".textarea-wrapper>textarea").classList.add("h-lvh");
87
+
88
+ document.querySelector("body").classList.add('overflow-hidden');
89
+ }
90
+ }
91
+
92
+ export default PreviewTool;
@@ -0,0 +1,16 @@
1
+ import MarkdownTool from './MarkdownTool.js';
2
+
3
+ class StrikethroughTool extends MarkdownTool {
4
+ constructor(editor) {
5
+ // Call the parent constructor with the markdown syntax for strikethrough (~~)
6
+ super(editor, '~~', 'strikethrough text');
7
+ this.button = this.createButton(`
8
+ <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
9
+ <path d="M16 4H9a3 3 0 0 0-2.83 4"/>
10
+ <path d="M14 12a4 4 0 0 1 0 8H6"/><line x1="4" x2="20" y1="12" y2="12"/>
11
+ </svg>
12
+ `);
13
+ }
14
+ }
15
+
16
+ export default StrikethroughTool;
@@ -0,0 +1,140 @@
1
+ // #markdown\editor.js
2
+ import './styles/main.css';
3
+ import { marked } from 'marked';
4
+ import Toolbar from './Toolbar/index.js';
5
+ import Preview from './preview.js';
6
+
7
+ marked.setOptions({
8
+ breaks: true
9
+ });
10
+
11
+ class markdownEditor {
12
+ constructor(selector, options = {}) {
13
+ this.usertextarea = typeof selector === 'string' ? document.querySelector(selector) : selector;
14
+ this.options = options;
15
+ this.preview = this.options.toolbar.includes('preview');
16
+ this.init();
17
+ }
18
+
19
+ init() {
20
+ this.createEditor();
21
+ if (this.options.toolbar) this.addToolbar();
22
+ }
23
+
24
+ createEditor() {
25
+ if (!this.isTextareaValid()) return;
26
+
27
+ this.applyDefaultAttributes();
28
+ this.buildEditorLayout();
29
+ this.addInputListener();
30
+ this.render();
31
+ }
32
+
33
+ isTextareaValid() {
34
+ return this.usertextarea.tagName === 'TEXTAREA';
35
+ }
36
+
37
+ applyDefaultAttributes() {
38
+ this.usertextarea.classList.add(
39
+ "p-1.5",
40
+ "max-w-full",
41
+ "size-full",
42
+ "bg-transparent",
43
+ "outline-0",
44
+ "appearance-none",
45
+ "prose",
46
+ "prose-sm",
47
+ "md:prose-base",
48
+ "dark:prose-invert",
49
+ "text-stone-700",
50
+ "dark:text-stone-200",
51
+ "overflow-y-auto"
52
+ );
53
+ if (!this.usertextarea.hasAttribute('placeholder')) {
54
+ this.usertextarea.placeholder = this.options.placeholder || 'Write your markdown...';
55
+ }
56
+ }
57
+
58
+ buildEditorLayout() {
59
+ this.editorContainer = document.createElement('div');
60
+ this.editorContainer.className = `
61
+ markdown-editor-wrapper
62
+ border border-stone-200
63
+ dark:border-stone-700
64
+ rounded-md
65
+ overflow-hidden
66
+ `;
67
+ this.usertextarea.parentNode.insertBefore(this.editorContainer, this.usertextarea);
68
+
69
+ this.markdownEditorDiv = document.createElement('div');
70
+ this.markdownEditorDiv.className = `editor-layout`;
71
+ this.editorContainer.appendChild(this.markdownEditorDiv);
72
+
73
+ this.addTextareaWrapper();
74
+ if (this.preview) this.addPreviewWrapper();
75
+ }
76
+
77
+ addTextareaWrapper() {
78
+ const textareaContainer = document.createElement('div');
79
+ textareaContainer.className = `
80
+ textarea-wrapper
81
+ p-2
82
+ bg-white
83
+ dark:bg-stone-800
84
+ grid
85
+ after:px-3.5
86
+ after:py-2.5
87
+ after:text-inherit
88
+ [&>textarea]:resize-none
89
+ [&>textarea]:[grid-area:1/1/2/2]
90
+ after:[grid-area:1/1/2/2]
91
+ after:whitespace-pre-wrap
92
+ after:invisible
93
+ after:content-[attr(data-cloned-val)_'_']
94
+ after:border
95
+ `;
96
+ textareaContainer.appendChild(this.usertextarea);
97
+ this.markdownEditorDiv.appendChild(textareaContainer);
98
+ }
99
+
100
+
101
+
102
+ addPreviewWrapper() {
103
+ const preview = new Preview(this.markdownEditorDiv);
104
+ this.previewContent = preview.getPreviewContent();
105
+ }
106
+
107
+ addInputListener() {
108
+ this.usertextarea.addEventListener('input', () => this.render());
109
+ this.usertextarea.addEventListener('scroll', () => {
110
+ const textarea = this.usertextarea;
111
+ const previewPane = this.previewContent;
112
+
113
+ // Calculate the proportion of the textarea that has been scrolled
114
+ const textareaScrollRatio = textarea.scrollTop / (textarea.scrollHeight - textarea.clientHeight);
115
+
116
+ // Apply the same scroll ratio to the preview pane
117
+ previewPane.scrollTop = textareaScrollRatio * (previewPane.scrollHeight - previewPane.clientHeight);
118
+ });
119
+ }
120
+
121
+ addToolbar() {
122
+ new Toolbar(this, this.options.toolbar || ['bold', 'italic', 'strikethrough']);
123
+ }
124
+
125
+ insertText(text) {
126
+ const { selectionStart, selectionEnd } = this.usertextarea;
127
+ const value = this.usertextarea.value;
128
+ this.usertextarea.value = `${value.substring(0, selectionStart)}${text}${value.substring(selectionEnd)}`;
129
+ this.usertextarea.focus();
130
+ this.usertextarea.setSelectionRange(selectionStart, selectionStart + text.length);
131
+ this.render();
132
+ }
133
+
134
+ render() {
135
+ const html = marked(this.usertextarea.value);
136
+ this.previewContent.innerHTML = html;
137
+ }
138
+ }
139
+
140
+ export default markdownEditor;
@@ -0,0 +1,17 @@
1
+ // markdown\preview.js
2
+ class Preview {
3
+ constructor(container) {
4
+ this.previewContainer = document.createElement('div');
5
+ this.previewContainer.className = 'preview-wrapper bg-white dark:bg-stone-800 p-2 hidden';
6
+ this.previewContent = document.createElement('div');
7
+ this.previewContent.className = 'preview-content prose prose-sm md:prose-base dark:prose-invert p-1.5 overflow-y-auto h-lvh max-w-max';
8
+ this.previewContainer.appendChild(this.previewContent);
9
+ container.appendChild(this.previewContainer);
10
+ }
11
+
12
+ getPreviewContent() {
13
+ return this.previewContent;
14
+ }
15
+ }
16
+
17
+ export default Preview;
@@ -0,0 +1,3 @@
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
@@ -0,0 +1,7 @@
1
+ // utils/markdownUtils.js
2
+
3
+ import { marked } from 'marked';
4
+
5
+ export function renderMarkdown(content) {
6
+ return marked(content);
7
+ }