Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selection events #6325

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}