Skip to content

Commit

Permalink
Fix selection events (#6325)
Browse files Browse the repository at this point in the history
* Pass options in setSelected

* Allow force sync from custom RTE
  • Loading branch information
artf authored Nov 22, 2024
1 parent 36572c3 commit fe28073
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
16 changes: 9 additions & 7 deletions packages/core/src/dom_components/view/ComponentTextView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bindAll } from 'underscore';
import { AddOptions, DisableOptions, ObjectAny, WithHTMLParserOptions } from '../../common';
import RichTextEditorModule from '../../rich_text_editor';
import RichTextEditorModule, { RteDisableResult } from '../../rich_text_editor';
import RichTextEditor from '../../rich_text_editor/model/RichTextEditor';
import { off, on } from '../../utils/dom';
import { getComponentModel } from '../../utils/mixins';
Expand Down Expand Up @@ -115,14 +115,17 @@ export default class ComponentTextView<TComp extends ComponentText = ComponentTe
const editable = model && model.get('editable');

if (rte) {
let disableRes: RteDisableResult = {};
const content = await this.getContent();

try {
await rte.disable(this, activeRte, opts);
disableRes = await rte.disable(this, activeRte, opts);
} catch (err) {
em.logError(err as any);
}

if (editable && (await this.getContent()) !== this.lastContent) {
await this.syncContent(opts);
if (editable && (content !== this.lastContent || disableRes.forceSync)) {
await this.syncContent({ ...opts, content });
this.lastContent = '';
}
}
Expand Down Expand Up @@ -151,7 +154,7 @@ export default class ComponentTextView<TComp extends ComponentText = ComponentTe
async syncContent(opts: ObjectAny = {}) {
const { model, rte, rteEnabled } = this;
if (!rteEnabled && !opts.force) return;
const content = await this.getContent();
const content = opts.content ?? (await this.getContent());
const comps = model.components();
const contentOpt: ObjectAny = { fromDisable: 1, ...opts };
model.set('content', '', contentOpt);
Expand Down Expand Up @@ -213,12 +216,11 @@ export default class ComponentTextView<TComp extends ComponentText = ComponentTe
* @param {Event} e
*/
onInput() {
const { em } = this;
const evPfx = 'component';
const ev = [`${evPfx}:update`, `${evPfx}:input`].join(' ');

// Update toolbars
em && em.trigger(ev, this.model);
this.em?.trigger(ev, this.model);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/editor/model/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,19 +555,19 @@ export default class EditorModel extends Model {

if (!isUndefined(min)) {
while (min !== index) {
this.addSelected(coll.at(min));
this.addSelected(coll.at(min), opts);
min++;
}
}

if (!isUndefined(max)) {
while (max !== index) {
this.addSelected(coll.at(max));
this.addSelected(coll.at(max), opts);
max--;
}
}

return this.addSelected(model);
return this.addSelected(model, opts);
}

!multiple && this.removeSelected(selected.filter((s) => s !== model));
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/rich_text_editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ interface ModelRTE {
currentView?: ComponentView;
}

export interface RteDisableResult {
forceSync?: boolean;
}

export default class RichTextEditorModule extends Module<RichTextEditorConfig & { pStylePrefix?: string }> {
pfx: string;
toolbar!: HTMLElement;
Expand Down Expand Up @@ -400,14 +404,18 @@ export default class RichTextEditorModule extends Module<RichTextEditorConfig &
* @param {Object} rte The instance of already defined RTE
* @private
* */
disable(view: ComponentView, rte?: RichTextEditor, opts: DisableOptions = {}) {
async disable(view: ComponentView, rte?: RichTextEditor, opts: DisableOptions = {}) {
let result: RteDisableResult = {};
const { em } = this;
const customRte = this.customRte;
// @ts-ignore
const el = view.getChildrenContainer();

if (customRte) {
customRte.disable(el, rte);
const res = await customRte.disable(el, rte);
if (res) {
result = res;
}
} else {
rte && rte.disable();
}
Expand All @@ -420,5 +428,7 @@ export default class RichTextEditorModule extends Module<RichTextEditorConfig &
}

this.model.unset('currentView');

return result;
}
}

0 comments on commit fe28073

Please sign in to comment.