MCPcopy Create free account
hub / github.com/NodeBB/nodebb-plugin-markdown / prepareFormattingTools

Function prepareFormattingTools

public/js/markdown.js:47–213  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

45};
46
47export function prepareFormattingTools() {
48 require([
49 'composer/formatting',
50 'composer/controls',
51 'translator',
52 ], function (formatting, controls, translator) {
53 if (formatting && controls) {
54 translator.getTranslations(window.config.userLang, 'markdown', function (strings) {
55 // used for h1,h2...h6
56 function formatHeading(heading, textarea, selectionStart, selectionEnd) {
57 if (selectionStart === selectionEnd) {
58 controls.insertIntoTextarea(textarea, `${heading} ${strings.heading}`);
59
60 controls.updateTextareaSelection(
61 textarea,
62 selectionStart + heading.length + 1,
63 selectionStart + strings.heading.length + heading.length + 1
64 );
65 } else {
66 const selectedText = $(textarea).val().substring(selectionStart, selectionEnd);
67 const newText = `${heading} ${selectedText}`;
68 controls.replaceSelectionInTextareaWith(textarea, newText);
69 controls.updateTextareaSelection(
70 textarea,
71 selectionStart + (heading.length + 1),
72 selectionEnd + (newText.length - selectedText.length)
73 );
74 }
75 }
76
77
78 formatting.addButtonDispatch('bold', function (textarea, selectionStart, selectionEnd) {
79 if (selectionStart === selectionEnd) {
80 const block = controls.getBlockData(textarea, '**', selectionStart);
81
82 if (block.in && block.atEnd) {
83 // At end of bolded string, move cursor past delimiters
84 controls.updateTextareaSelection(textarea, selectionStart + 2, selectionStart + 2);
85 } else {
86 controls.insertIntoTextarea(textarea, '**' + strings.bold + '**');
87 controls.updateTextareaSelection(
88 textarea, selectionStart + 2, selectionStart + strings.bold.length + 2
89 );
90 }
91 } else {
92 const wrapDelta = controls.wrapSelectionInTextareaWith(textarea, '**');
93 controls.updateTextareaSelection(
94 textarea, selectionStart + 2 + wrapDelta[0], selectionEnd + 2 - wrapDelta[1]
95 );
96 }
97 });
98
99 formatting.addButtonDispatch('italic', function (textarea, selectionStart, selectionEnd) {
100 if (selectionStart === selectionEnd) {
101 const block = controls.getBlockData(textarea, '*', selectionStart);
102
103 if (block.in && block.atEnd) {
104 // At end of italicised string, move cursor past delimiters

Callers

nothing calls this directly

Calls 1

formatHeadingFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…