2023-08-24 20:49:24 -04:00
|
|
|
import Component from "@glimmer/component";
|
2024-08-26 01:43:40 -04:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2023-08-24 20:49:24 -04:00
|
|
|
import { action } from "@ember/object";
|
2024-08-26 01:43:40 -04:00
|
|
|
import { next } from "@ember/runloop";
|
|
|
|
import { inject as service } from "@ember/service";
|
2023-11-03 07:30:09 -04:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2024-08-26 01:43:40 -04:00
|
|
|
import CookText from "discourse/components/cook-text";
|
2023-08-24 20:49:24 -04:00
|
|
|
import DButton from "discourse/components/d-button";
|
|
|
|
import DModal from "discourse/components/d-modal";
|
2024-08-26 01:43:40 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-01-12 18:28:06 -05:00
|
|
|
import i18n from "discourse-common/helpers/i18n";
|
2023-08-24 20:49:24 -04:00
|
|
|
|
|
|
|
export default class ModalDiffModal extends Component {
|
2024-08-26 01:43:40 -04:00
|
|
|
@service currentUser;
|
|
|
|
@tracked loading = false;
|
|
|
|
@tracked diff;
|
|
|
|
suggestion = "";
|
|
|
|
|
|
|
|
PROOFREAD_ID = -303;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.diff = this.args.model.diff;
|
|
|
|
|
|
|
|
next(() => {
|
|
|
|
if (this.args.model.toolbarEvent) {
|
|
|
|
this.loadDiff();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadDiff() {
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const suggestion = await ajax("/discourse-ai/ai-helper/suggest", {
|
|
|
|
method: "POST",
|
|
|
|
data: {
|
|
|
|
mode: this.PROOFREAD_ID,
|
|
|
|
text: this.selectedText,
|
|
|
|
force_default_locale: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.diff = suggestion.diff;
|
|
|
|
this.suggestion = suggestion.suggestions[0];
|
|
|
|
} catch (e) {
|
|
|
|
popupAjaxError(e);
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedText() {
|
|
|
|
const selected = this.args.model.toolbarEvent.selected;
|
|
|
|
|
|
|
|
if (selected.value === "") {
|
|
|
|
return selected.pre + selected.post;
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected.value;
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:30:09 -04:00
|
|
|
@action
|
|
|
|
triggerConfirmChanges() {
|
|
|
|
this.args.closeModal();
|
2024-08-26 01:43:40 -04:00
|
|
|
if (this.args.model.confirm) {
|
|
|
|
this.args.model.confirm();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.args.model.toolbarEvent && this.suggestion) {
|
|
|
|
this.args.model.toolbarEvent.replaceText(
|
|
|
|
this.selectedText,
|
|
|
|
this.suggestion
|
|
|
|
);
|
|
|
|
}
|
2023-11-03 07:30:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
triggerRevertChanges() {
|
2024-08-16 13:08:58 -04:00
|
|
|
this.args.model.revert();
|
2023-11-03 07:30:09 -04:00
|
|
|
this.args.closeModal();
|
|
|
|
}
|
|
|
|
|
2023-08-24 20:49:24 -04:00
|
|
|
<template>
|
|
|
|
<DModal
|
|
|
|
class="composer-ai-helper-modal"
|
2024-01-12 18:28:06 -05:00
|
|
|
@title={{i18n "discourse_ai.ai_helper.context_menu.changes"}}
|
2023-08-24 20:49:24 -04:00
|
|
|
@closeModal={{@closeModal}}
|
|
|
|
>
|
|
|
|
<:body>
|
2024-08-26 01:43:40 -04:00
|
|
|
{{#if this.loading}}
|
|
|
|
<div class="composer-ai-helper-modal__loading">
|
|
|
|
<CookText @rawText={{this.selectedText}} />
|
2023-08-24 20:49:24 -04:00
|
|
|
</div>
|
2024-08-26 01:43:40 -04:00
|
|
|
{{else}}
|
|
|
|
{{#if this.diff}}
|
|
|
|
{{htmlSafe this.diff}}
|
|
|
|
{{else}}
|
|
|
|
<div class="composer-ai-helper-modal__old-value">
|
|
|
|
{{@model.oldValue}}
|
|
|
|
</div>
|
2023-08-24 20:49:24 -04:00
|
|
|
|
2024-08-26 01:43:40 -04:00
|
|
|
<div class="composer-ai-helper-modal__new-value">
|
|
|
|
{{@model.newValue}}
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
2023-08-24 20:49:24 -04:00
|
|
|
{{/if}}
|
|
|
|
</:body>
|
|
|
|
|
|
|
|
<:footer>
|
2024-08-26 01:43:40 -04:00
|
|
|
{{#if this.loading}}
|
|
|
|
<DButton
|
|
|
|
class="btn-primary"
|
|
|
|
@label="discourse_ai.ai_helper.context_menu.loading"
|
|
|
|
@disabled={{true}}
|
|
|
|
/>
|
|
|
|
{{else}}
|
|
|
|
<DButton
|
|
|
|
class="btn-primary confirm"
|
|
|
|
@action={{this.triggerConfirmChanges}}
|
|
|
|
@label="discourse_ai.ai_helper.context_menu.confirm"
|
|
|
|
/>
|
|
|
|
{{#if @model.revert}}
|
|
|
|
<DButton
|
|
|
|
class="btn-flat revert"
|
|
|
|
@action={{this.triggerRevertChanges}}
|
|
|
|
@label="discourse_ai.ai_helper.context_menu.revert"
|
|
|
|
/>
|
|
|
|
{{/if}}
|
|
|
|
{{/if}}
|
2023-08-24 20:49:24 -04:00
|
|
|
</:footer>
|
|
|
|
</DModal>
|
|
|
|
</template>
|
|
|
|
}
|