DEV: Update discourse-local-dates to new Modal API (#23560)

This commit is contained in:
David Taylor 2023-09-13 15:00:38 +01:00 committed by GitHub
parent e4849445ce
commit 8a57798419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 226 additions and 240 deletions

View File

@ -5,7 +5,6 @@ import { tracked } from "@glimmer/tracking";
import { cancel, next } from "@ember/runloop"; import { cancel, next } from "@ember/runloop";
import { cloneJSON } from "discourse-common/lib/object"; import { cloneJSON } from "discourse-common/lib/object";
import { chatComposerButtons } from "discourse/plugins/chat/discourse/lib/chat-composer-buttons"; import { chatComposerButtons } from "discourse/plugins/chat/discourse/lib/chat-composer-buttons";
import showModal from "discourse/lib/show-modal";
import TextareaInteractor from "discourse/plugins/chat/discourse/lib/textarea-interactor"; import TextareaInteractor from "discourse/plugins/chat/discourse/lib/textarea-interactor";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import userSearch from "discourse/lib/user-search"; import userSearch from "discourse/lib/user-search";
@ -166,7 +165,12 @@ export default class ChatComposer extends Component {
@action @action
insertDiscourseLocalDate() { insertDiscourseLocalDate() {
showModal("discourse-local-dates-create-modal").setProperties({ // JIT import because local-dates isn't necessarily enabled
const LocalDatesCreateModal =
require("discourse/plugins/discourse-local-dates/discourse/components/modal/local-dates-create").default;
this.modal.show(LocalDatesCreateModal, {
model: {
insertDate: (markup) => { insertDate: (markup) => {
this.composer.textarea.addText( this.composer.textarea.addText(
this.composer.textarea.getSelected(), this.composer.textarea.getSelected(),
@ -174,6 +178,7 @@ export default class ChatComposer extends Component {
); );
this.composer.focus(); this.composer.focus();
}, },
},
}); });
} }

View File

@ -0,0 +1,200 @@
<DModal
@title={{i18n "discourse_local_dates.title"}}
@closeModal={{@closeModal}}
class="discourse-local-dates-create-modal"
>
<:body>
<div class="form">
{{#if this.isValid}}
{{#if this.timezoneIsDifferentFromUserTimezone}}
<div class="preview alert alert-info">
{{i18n "discourse_local_dates.create.form.current_timezone"}}
<b>{{this.formattedCurrentUserTimezone}}</b>{{this.currentPreview}}
</div>
{{/if}}
{{else}}
<div class="validation-error alert alert-error">
{{i18n "discourse_local_dates.create.form.invalid_date"}}
</div>
{{/if}}
{{this.computeDate}}
<div class="date-time-configuration">
<div class="inputs-panel">
<div
class="date-time-control from
{{if this.fromSelected 'is-selected'}}
{{if this.fromFilled 'is-filled'}}"
>
{{d-icon "calendar-alt"}}
<DButton
@action={{this.focusFrom}}
@translatedLabel={{this.formattedFrom}}
id="from-date-time"
class="date-time"
autofocus
/>
</div>
<div
class="date-time-control to
{{if this.toSelected 'is-selected'}}
{{if this.toFilled 'is-filled'}}"
>
{{d-icon "calendar-alt"}}
<DButton
@action={{this.focusTo}}
@translatedLabel={{this.formattedTo}}
class="date-time"
/>
{{#if this.toFilled}}
<DButton
@action={{this.eraseToDateTime}}
@icon="times"
class="delete-to-date"
/>
{{/if}}
</div>
{{#unless this.site.mobileView}}
<TimezoneInput
@options={{hash icon="globe"}}
@value={{this.timezone}}
@onChange={{action (mut this.timezone)}}
/>
{{/unless}}
</div>
<div class="picker-panel">
<CalendarDateTimeInput
@datePickerId="local-date-create-form"
@date={{this.selectedDate}}
@time={{this.selectedTime}}
@minDate={{this.minDate}}
@timeFormat={{this.timeFormat}}
@dateFormat={{this.dateFormat}}
@onChangeDate={{action this.changeSelectedDate}}
@onChangeTime={{action this.changeSelectedTime}}
/>
</div>
{{#if this.site.mobileView}}
<TimezoneInput
@value={{this.timezone}}
@options={{hash icon="globe"}}
@onChange={{action (mut this.timezone)}}
/>
{{/if}}
</div>
{{#if this.advancedMode}}
<div class="advanced-options">
{{#unless this.isRange}}
<div class="control-group recurrence">
<label class="control-label">
{{i18n "discourse_local_dates.create.form.recurring_title"}}
</label>
<p>{{html-safe
(i18n
"discourse_local_dates.create.form.recurring_description"
)
}}</p>
<div class="controls">
<ComboBox
@content={{this.recurringOptions}}
@class="recurrence-input"
@value={{this.recurring}}
@onChange={{action (mut this.recurring)}}
@options={{hash
none="discourse_local_dates.create.form.recurring_none"
}}
/>
</div>
</div>
{{/unless}}
<div class="control-group timezones">
<label>{{i18n
"discourse_local_dates.create.form.timezones_title"
}}</label>
<p>{{i18n
"discourse_local_dates.create.form.timezones_description"
}}</p>
<div class="controls">
<MultiSelect
@valueProperty={{null}}
@nameProperty={{null}}
@class="timezones-input"
@content={{this.allTimezones}}
@value={{this.timezones}}
@options={{hash allowAny=false maximum=5}}
/>
</div>
</div>
<div class="control-group format">
<label>{{i18n
"discourse_local_dates.create.form.format_title"
}}</label>
<p>
{{i18n "discourse_local_dates.create.form.format_description"}}
<a
target="_blank"
href="https://momentjs.com/docs/#/parsing/string-format/"
rel="noopener noreferrer"
>
{{d-icon "question-circle"}}
</a>
</p>
<div class="controls">
<TextField @value={{this.format}} @class="format-input" />
</div>
</div>
<div class="control-group">
<ul class="formats">
{{#each this.previewedFormats as |previewedFormat|}}
<li class="format">
<a
class="moment-format"
href
{{on "click" (fn this.updateFormat previewedFormat.format)}}
>
{{previewedFormat.format}}
</a>
<span class="previewed-format">
{{previewedFormat.preview}}
</span>
</li>
{{/each}}
</ul>
</div>
</div>
{{/if}}
</div>
</:body>
<:footer>
{{#if this.isValid}}
<DButton
@action={{this.save}}
@label="discourse_local_dates.create.form.insert"
class="btn-primary"
/>
{{/if}}
<DButton
@action={{this.cancel}}
@translatedLabel={{i18n "cancel"}}
class="btn-flat"
/>
<DButton
@action={{this.toggleAdvancedMode}}
@icon="cog"
@label={{this.toggleModeBtnLabel}}
class="btn-default advanced-mode-btn"
/>
</:footer>
</DModal>

View File

@ -10,7 +10,6 @@ import { cookAsync } from "discourse/lib/text";
import { notEmpty } from "@ember/object/computed"; import { notEmpty } from "@ember/object/computed";
import { propertyNotEqual } from "discourse/lib/computed"; import { propertyNotEqual } from "discourse/lib/computed";
import { schedule } from "@ember/runloop"; import { schedule } from "@ember/runloop";
import { getOwner } from "discourse-common/lib/get-owner";
import { applyLocalDates } from "discourse/lib/local-dates"; import { applyLocalDates } from "discourse/lib/local-dates";
import generateDateMarkup from "discourse/plugins/discourse-local-dates/lib/local-date-markup-generator"; import generateDateMarkup from "discourse/plugins/discourse-local-dates/lib/local-date-markup-generator";
@ -369,17 +368,13 @@ export default Component.extend({
const markup = this.markup; const markup = this.markup;
if (markup) { if (markup) {
this._closeModal(); this.closeModal();
this.insertDate(markup); this.model.insertDate(markup);
} }
}, },
@action @action
cancel() { cancel() {
this._closeModal(); this.closeModal();
},
_closeModal() {
getOwner(this).lookup("service:modal").close();
}, },
}); });

View File

@ -1,21 +0,0 @@
import Controller from "@ember/controller";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { schedule } from "@ember/runloop";
export default Controller.extend(ModalFunctionality, {
onShow() {
schedule("afterRender", () => {
const fromButton = document.getElementById("from-date-time");
fromButton && fromButton.focus();
});
},
onClose() {
schedule("afterRender", () => {
const localDatesBtn = document.querySelector(
".d-editor-button-bar .local-dates.btn"
);
localDatesBtn && localDatesBtn.focus();
});
},
});

View File

@ -1,194 +0,0 @@
<DModalBody
@title="discourse_local_dates.title"
@class="discourse-local-dates-create-modal"
@style="overflow: auto"
>
<div class="form">
{{#if this.isValid}}
{{#if this.timezoneIsDifferentFromUserTimezone}}
<div class="preview alert alert-info">
{{i18n "discourse_local_dates.create.form.current_timezone"}}
<b>{{this.formattedCurrentUserTimezone}}</b>{{this.currentPreview}}
</div>
{{/if}}
{{else}}
<div class="validation-error alert alert-error">
{{i18n "discourse_local_dates.create.form.invalid_date"}}
</div>
{{/if}}
{{this.computeDate}}
<div class="date-time-configuration">
<div class="inputs-panel">
<div
class="date-time-control from
{{if this.fromSelected 'is-selected'}}
{{if this.fromFilled 'is-filled'}}"
>
{{d-icon "calendar-alt"}}
<DButton
@action={{this.focusFrom}}
@translatedLabel={{this.formattedFrom}}
id="from-date-time"
class="date-time"
/>
</div>
<div
class="date-time-control to
{{if this.toSelected 'is-selected'}}
{{if this.toFilled 'is-filled'}}"
>
{{d-icon "calendar-alt"}}
<DButton
@action={{this.focusTo}}
@translatedLabel={{this.formattedTo}}
class="date-time"
/>
{{#if this.toFilled}}
<DButton
@action={{this.eraseToDateTime}}
@icon="times"
class="delete-to-date"
/>
{{/if}}
</div>
{{#unless this.site.mobileView}}
<TimezoneInput
@options={{hash icon="globe"}}
@value={{this.timezone}}
@onChange={{action (mut this.timezone)}}
/>
{{/unless}}
</div>
<div class="picker-panel">
<CalendarDateTimeInput
@datePickerId="local-date-create-form"
@date={{this.selectedDate}}
@time={{this.selectedTime}}
@minDate={{this.minDate}}
@timeFormat={{this.timeFormat}}
@dateFormat={{this.dateFormat}}
@onChangeDate={{action this.changeSelectedDate}}
@onChangeTime={{action this.changeSelectedTime}}
/>
</div>
{{#if this.site.mobileView}}
<TimezoneInput
@value={{this.timezone}}
@options={{hash icon="globe"}}
@onChange={{action (mut this.timezone)}}
/>
{{/if}}
</div>
{{#if this.advancedMode}}
<div class="advanced-options">
{{#unless this.isRange}}
<div class="control-group recurrence">
<label class="control-label">
{{i18n "discourse_local_dates.create.form.recurring_title"}}
</label>
<p>{{html-safe
(i18n "discourse_local_dates.create.form.recurring_description")
}}</p>
<div class="controls">
<ComboBox
@content={{this.recurringOptions}}
@class="recurrence-input"
@value={{this.recurring}}
@onChange={{action (mut this.recurring)}}
@options={{hash
none="discourse_local_dates.create.form.recurring_none"
}}
/>
</div>
</div>
{{/unless}}
<div class="control-group timezones">
<label>{{i18n
"discourse_local_dates.create.form.timezones_title"
}}</label>
<p>{{i18n
"discourse_local_dates.create.form.timezones_description"
}}</p>
<div class="controls">
<MultiSelect
@valueProperty={{null}}
@nameProperty={{null}}
@class="timezones-input"
@content={{this.allTimezones}}
@value={{this.timezones}}
@options={{hash allowAny=false maximum=5}}
/>
</div>
</div>
<div class="control-group format">
<label>{{i18n
"discourse_local_dates.create.form.format_title"
}}</label>
<p>
{{i18n "discourse_local_dates.create.form.format_description"}}
<a
target="_blank"
href="https://momentjs.com/docs/#/parsing/string-format/"
rel="noopener noreferrer"
>
{{d-icon "question-circle"}}
</a>
</p>
<div class="controls">
<TextField @value={{this.format}} @class="format-input" />
</div>
</div>
<div class="control-group">
<ul class="formats">
{{#each this.previewedFormats as |previewedFormat|}}
<li class="format">
<a
class="moment-format"
href
{{on "click" (fn this.updateFormat previewedFormat.format)}}
>
{{previewedFormat.format}}
</a>
<span class="previewed-format">
{{previewedFormat.preview}}
</span>
</li>
{{/each}}
</ul>
</div>
</div>
{{/if}}
</div>
</DModalBody>
<div class="modal-footer discourse-local-dates-create-modal-footer">
{{#if this.isValid}}
<DButton
@action={{this.save}}
@label="discourse_local_dates.create.form.insert"
class="btn-primary"
/>
{{/if}}
<DButton
@action={{this.cancel}}
@translatedLabel={{i18n "cancel"}}
class="btn-flat"
/>
<DButton
@action={{this.toggleAdvancedMode}}
@icon="cog"
@label={{this.toggleModeBtnLabel}}
class="btn-default advanced-mode-btn"
/>
</div>

View File

@ -1,4 +0,0 @@
<DiscourseLocalDatesCreateForm
@config={{this.config}}
@insertDate={{this.insertDate}}
/>

View File

@ -2,7 +2,7 @@ import deprecated from "discourse-common/lib/deprecated";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import LocalDateBuilder from "../lib/local-date-builder"; import LocalDateBuilder from "../lib/local-date-builder";
import { withPluginApi } from "discourse/lib/plugin-api"; import { withPluginApi } from "discourse/lib/plugin-api";
import showModal from "discourse/lib/show-modal"; import LocalDatesCreateModal from "../discourse/components/modal/local-dates-create";
import { downloadCalendar } from "discourse/lib/download-calendar"; import { downloadCalendar } from "discourse/lib/download-calendar";
import { renderIcon } from "discourse-common/lib/icon-library"; import { renderIcon } from "discourse-common/lib/icon-library";
import I18n from "I18n"; import I18n from "I18n";
@ -12,6 +12,7 @@ import {
addTextDecorateCallback, addTextDecorateCallback,
} from "discourse/lib/to-markdown"; } from "discourse/lib/to-markdown";
import generateDateMarkup from "discourse/plugins/discourse-local-dates/lib/local-date-markup-generator"; import generateDateMarkup from "discourse/plugins/discourse-local-dates/lib/local-date-markup-generator";
import { inject as service } from "@ember/service";
// Import applyLocalDates from discourse/lib/local-dates instead // Import applyLocalDates from discourse/lib/local-dates instead
export function applyLocalDates(dates, siteSettings) { export function applyLocalDates(dates, siteSettings) {
@ -173,13 +174,16 @@ function initializeDiscourseLocalDates(api) {
}); });
api.modifyClass("component:d-editor", { api.modifyClass("component:d-editor", {
modal: service(),
pluginId: "discourse-local-dates", pluginId: "discourse-local-dates",
actions: { actions: {
insertDiscourseLocalDate(toolbarEvent) { insertDiscourseLocalDate(toolbarEvent) {
showModal("discourse-local-dates-create-modal").setProperties({ this.modal.show(LocalDatesCreateModal, {
model: {
insertDate: (markup) => { insertDate: (markup) => {
toolbarEvent.addText(markup); toolbarEvent.addText(markup);
}, },
},
}); });
}, },
}, },

View File

@ -48,7 +48,7 @@ div[data-tippy-root] {
} }
} }
.discourse-local-dates-create-modal-footer { .discourse-local-dates-create-modal .modal-footer {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: flex-start; justify-content: flex-start;
@ -69,6 +69,7 @@ div[data-tippy-root] {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
padding: 0.5em; padding: 0.5em;
overflow: auto;
.form { .form {
flex: 1 0 0px; flex: 1 0 0px;