DEV: refactor composer-editor/d-editor, a little more (#29973)

Adds setupEditor to ComposerEditor so it can setup/destroy events when the underlying editorComponent is switched.

Moves putCursorAtEnd uses (which implementation is textarea-specific) to TextareaTextManipulation.

Moves insertCurrentTime and a corresponding test, which is discourse-local-dates specific, to the plugin.

Moves applyList and formatCode from DEditor to the TextareaTextManipulation.

Moves DEditor._applySurround to TextareaTextManipulation.applySurroundSelection

Avoids resetting the textarea value on applyList and formatCode, keeping the undo history.
This commit is contained in:
Renato Atilio
2024-12-02 18:24:14 -03:00
committed by GitHub
parent 706987ce76
commit 85691a7f31
9 changed files with 216 additions and 168 deletions

View File

@ -142,6 +142,7 @@ function _partitionedRanges(element) {
}
function initializeDiscourseLocalDates(api) {
const modal = api.container.lookup("service:modal");
const siteSettings = api.container.lookup("service:site-settings");
const defaultTitle = i18n("discourse_local_dates.default_title", {
site_name: siteSettings.title,
@ -164,25 +165,19 @@ function initializeDiscourseLocalDates(api) {
id: "local-dates",
group: "extras",
icon: "calendar-days",
sendAction: (event) =>
toolbar.context.send("insertDiscourseLocalDate", event),
});
});
perform: (event) =>
modal.show(LocalDatesCreateModal, {
model: { insertDate: (markup) => event.addText(markup) },
}),
shortcut: "Shift+.",
shortcutAction: (event) => {
const timezone = api.getCurrentUser().user_option.timezone;
const time = moment().format("HH:mm:ss");
const date = moment().format("YYYY-MM-DD");
api.modifyClass("component:d-editor", {
modal: service(),
pluginId: "discourse-local-dates",
actions: {
insertDiscourseLocalDate(toolbarEvent) {
this.modal.show(LocalDatesCreateModal, {
model: {
insertDate: (markup) => {
toolbarEvent.addText(markup);
},
},
});
event.addText(`[date=${date} time=${time} timezone="${timezone}"]`);
},
},
});
});
addTextDecorateCallback(function (