UNPKG

markdown-text-editor

Version:

A simple JavaScript Markdown editor plugin with real-time preview, and easy integration.

34 lines (28 loc) 1.4 kB
import MakeTool from '../MakeTool.js'; class BoldTool extends MakeTool { constructor(editor) { super(editor, 'Bold'); this.button = this.createButton(` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"> <path d="M8 11H12.5C13.8807 11 15 9.88071 15 8.5C15 7.11929 13.8807 6 12.5 6H8V11ZM18 15.5C18 17.9853 15.9853 20 13.5 20H6V4H12.5C14.9853 4 17 6.01472 17 8.5C17 9.70431 16.5269 10.7981 15.7564 11.6058C17.0979 12.3847 18 13.837 18 15.5ZM8 13V18H13.5C14.8807 18 16 16.8807 16 15.5C16 14.1193 14.8807 13 13.5 13H8Z"></path> </svg> `) } // Apply bold syntax (**bold**) applySyntax() { const textarea = this.editor.usertextarea; const { selectionStart, selectionEnd } = textarea; const selectedText = textarea.value.substring(selectionStart, selectionEnd); const syntax = '**'; let newText = ''; if (selectedText.startsWith(syntax) && selectedText.endsWith(syntax)) { // Remove the bold syntax if it's already wrapped newText = selectedText.slice(syntax.length, -syntax.length); } else { // Apply bold syntax newText = `${syntax}${selectedText || 'Bold text'}${syntax}`; } this.editor.insertText(newText); } } export default BoldTool;