|
| 1 | +import { apiInitializer } from "discourse/lib/api"; |
| 2 | +import loadScript from "discourse/lib/load-script"; |
| 3 | +import I18n from "I18n"; |
| 4 | + |
| 5 | +async function applyHighlight(element) { |
| 6 | + const highlights = element.querySelectorAll("mark"); |
| 7 | + if (!highlights.length) { |
| 8 | + return; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +// Helper function to get raw text without translation |
| 13 | +function getRawText(text) { |
| 14 | + return text.replace(/\[.*?\]/g, ''); |
| 15 | +} |
| 16 | + |
| 17 | +export default apiInitializer("0.11.1", (api) => { |
| 18 | + const { iconNode } = require("discourse-common/lib/icon-library"); |
| 19 | + const currentLocale = I18n.currentLocale(); |
| 20 | + |
| 21 | + // Localization setup - keep only the button titles in translations |
| 22 | + I18n.translations[currentLocale].js.underline_button_title = settings.underline_button; |
| 23 | + I18n.translations[currentLocale].js.align_center_button_title = settings.align_center_button; |
| 24 | + I18n.translations[currentLocale].js.align_right_button_title = settings.align_right_button; |
| 25 | + I18n.translations[currentLocale].js.align_justify_button_title = settings.align_justify_button; |
| 26 | + I18n.translations[currentLocale].js.strikethrough_button_title = settings.strikethrough_button; |
| 27 | + I18n.translations[currentLocale].js.superscript_button_title = settings.superscript_button; |
| 28 | + I18n.translations[currentLocale].js.subscript_button_title = settings.subscript_button; |
| 29 | + I18n.translations[currentLocale].js.columns_button_title = settings.columns_button; |
| 30 | + I18n.translations[currentLocale].js.align_left_button_title = settings.align_left_button; |
| 31 | + I18n.translations[currentLocale].js.float_left_button = settings.float_left_button; |
| 32 | + I18n.translations[currentLocale].js.highlight_button_title = settings.highlighter_button; |
| 33 | + I18n.translations[currentLocale].js.composer.highlighter_text = settings.highlighter_text; |
| 34 | + I18n.translations[currentLocale].js.composer.underline_text = settings.underline_text; |
| 35 | + I18n.translations[currentLocale].js.composer.align_center_text = settings.align_center_text; |
| 36 | + I18n.translations[currentLocale].js.composer.align_left_text = settings.align_left_text; |
| 37 | + I18n.translations[currentLocale].js.composer.align_right_text = settings.align_right_text; |
| 38 | + I18n.translations[currentLocale].js.composer.align_justify_text = settings.align_justify_text; |
| 39 | + I18n.translations[currentLocale].js.composer.strikethrough_text = settings.strikethrough_text; |
| 40 | + I18n.translations[currentLocale].js.composer.superscript_text = settings.superscript_text; |
| 41 | + I18n.translations[currentLocale].js.composer.subscript_text = settings.subscript_text; |
| 42 | + I18n.translations[currentLocale].js.composer.columns_text = settings.columns_text; |
| 43 | + I18n.translations[currentLocale].js.composer.float_left_text = settings.float_left_text; |
| 44 | + |
| 45 | + // Toolbar Button Definitions |
| 46 | + api.onToolbarCreate((toolbar) => { |
| 47 | + const buttons = [ |
| 48 | + { |
| 49 | + id: "underline_button", |
| 50 | + group: "fontStyles", |
| 51 | + icon: "underline", |
| 52 | + shortcut: "U", |
| 53 | + title: "underline_button_title", |
| 54 | + perform: (e) => e.applySurround("[u]", "[/u]", "underline_text"), |
| 55 | + }, |
| 56 | + { |
| 57 | + id: "strikethrough_button", |
| 58 | + group: "fontStyles", |
| 59 | + icon: "strikethrough", |
| 60 | + title: "strikethrough_button_title", |
| 61 | + perform: (e) => e.applySurround("<s>", "</s>", "strikethrough_text"), |
| 62 | + }, |
| 63 | + { |
| 64 | + id: "superscript_button", |
| 65 | + group: "fontStyles", |
| 66 | + icon: "superscript", |
| 67 | + title: "superscript_button_title", |
| 68 | + perform: (e) => e.applySurround("<sup>", "</sup>", "superscript_text"), |
| 69 | + }, |
| 70 | + { |
| 71 | + id: "subscript_button", |
| 72 | + group: "fontStyles", |
| 73 | + icon: "subscript", |
| 74 | + title: "subscript_button_title", |
| 75 | + perform: (e) => e.applySurround("<sub>", "</sub>", "subscript_text"), |
| 76 | + }, |
| 77 | + { |
| 78 | + id: "align_left_button", |
| 79 | + group: "extras", |
| 80 | + icon: "align-left", |
| 81 | + title: "align_left_button_title", |
| 82 | + perform: (e) => e.applySurround('[wrap="left"]\n', "\n[/wrap]", "float_left_text"), |
| 83 | + }, |
| 84 | + { |
| 85 | + id: "align_center_button", |
| 86 | + group: "extras", |
| 87 | + icon: "align-center", |
| 88 | + title: "align_center_button_title", |
| 89 | + perform: (e) => e.applySurround('[wrap="center"]\n', "\n[/wrap]", "align_center_text"), |
| 90 | + }, |
| 91 | + { |
| 92 | + id: "align_right_button", |
| 93 | + group: "extras", |
| 94 | + icon: "align-right", |
| 95 | + title: "align_right_button_title", |
| 96 | + perform: (e) => e.applySurround('[wrap="right"]\n', "\n[/wrap]", "align_right_text"), |
| 97 | + }, |
| 98 | + { |
| 99 | + id: "align_justify_button", |
| 100 | + group: "extras", |
| 101 | + icon: "align-justify", |
| 102 | + title: "align_justify_button_title", |
| 103 | + perform: (e) => e.applySurround('[wrap="justify"]\n', "\n[/wrap]", "align_justify_text"), |
| 104 | + }, |
| 105 | + ]; |
| 106 | + |
| 107 | + buttons.forEach((button) => toolbar.addButton(button)); |
| 108 | + }); |
| 109 | + |
| 110 | + api.addComposerToolbarPopupMenuOption({ |
| 111 | + action: (toolbarEvent) => { |
| 112 | + toolbarEvent.applySurround('<mark>', '</mark>', "highlighter_text"); |
| 113 | + }, |
| 114 | + icon: "highlighter", |
| 115 | + label: "highlight_button_title", |
| 116 | + shortcut: "H", |
| 117 | + }); |
| 118 | + |
| 119 | + api.addComposerToolbarPopupMenuOption({ |
| 120 | + action: (toolbarEvent) => |
| 121 | + toolbarEvent.applySurround('[wrap="columns"]\n', "\n[/wrap]", "columns_text"), |
| 122 | + icon: "table-columns", |
| 123 | + label: "columns_button_title", |
| 124 | + }); |
| 125 | + |
| 126 | + api.addComposerToolbarPopupMenuOption({ |
| 127 | + action: (toolbarEvent) => { |
| 128 | + toolbarEvent.applySurround('[wrap="floatl"]\n', '\n[/wrap]', "float_left_text"); |
| 129 | + }, |
| 130 | + icon: 'indent', |
| 131 | + label: 'float_left_button', |
| 132 | + }); |
| 133 | + |
| 134 | + // Decorate cooked elements with highlight processing |
| 135 | + api.decorateCookedElement( |
| 136 | + async (elem, helper) => { |
| 137 | + const id = helper ? `post_${helper.getModel().id}` : "composer"; |
| 138 | + applyHighlight(elem, id); |
| 139 | + }, |
| 140 | + { id: "wrap-mark" } |
| 141 | + ); |
| 142 | +}); |
0 commit comments