Skip to content

Commit

Permalink
fix(entity-reference): fix handling overlapping marks on click event (#…
Browse files Browse the repository at this point in the history
…1794)

* fix(entity-reference): fix handling overlapping marks on click event

The extension uses `click` event instead of `clickMark`
The `onClick` callback receives the mark attributes, it's range and it's text as opposed to just the attributes
Fix handling of overlapping marks on click position

Co-authored-by: Idriss mahjoubi <mahjoubi.idriss@gmail.com>
  • Loading branch information
IdrissMahjoubi and Idriss mahjoubi committed Jul 20, 2022
1 parent 1272622 commit f720dfe
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-bulldogs-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remirror/extension-entity-reference': minor
---

Update pnpm-lock.yaml
5 changes: 5 additions & 0 deletions .changeset/twelve-meals-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remirror/extension-entity-reference': minor
---

Click event receives all entity reference marks, their ranges and their respective text on the clicked position
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ describe('EntityReference marks', () => {

view.someProp('handleClickOn', (fn) => fn(view, 2, node, 1, {} as MouseEvent, false));
expect(onClickMark).toHaveBeenCalledTimes(1);
expect(onClickMark).toHaveBeenCalledWith(entityReference.id);
expect(onClickMark).toHaveBeenCalledWith([entityReference]);
});

it('responds to clicks and passes no argument if click is not a mark', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
{
"path": "../../remirror__core/src"
},
{
"path": "../../remirror__extension-events/src"
},
{
"path": "../../remirror__extension-positioner/src"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
CreateExtensionPlugin,
EditorState,
extension,
GetMarkRange,
getTextSelection,
Helper,
helper,
Expand All @@ -30,7 +29,6 @@ import { EntityReferenceMetaData, EntityReferenceOptions } from './types';
import { decorateEntityReferences } from './utils/decorate-entity-references';
import { getDisjoinedEntityReferencesFromNode } from './utils/disjoined-entity-references';
import { joinDisjoinedEntityReferences } from './utils/joined-entity-references';
import { getShortestEntityReference } from './utils/shortest-entity-reference';

/**
* Required props to create entityReference marks decorations.
Expand Down Expand Up @@ -118,18 +116,14 @@ export class EntityReferenceExtension extends MarkExtension<EntityReferenceOptio
* 2. the id of the clicked entity reference
* 3. id of the shortest entity reference in case of overlapping entities
*/
clickMark: (_event, clickState) => {
const { markRanges } = clickState;

const entityReferences = markRanges.filter(({ mark }) => mark.type.name === this.name);
click: (_event, clickState) => {
const entityReferences = this.getEntityReferencesAt(clickState.pos);

if (entityReferences.length === 0) {
return this.options.onClickMark();
}

const shortestMark = getShortestEntityReference(entityReferences) as GetMarkRange;

return this.options.onClickMark(shortestMark.mark.attrs.id);
return this.options.onClickMark(entityReferences);
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
{
"path": "../../remirror__core/src"
},
{
"path": "../../remirror__extension-events/src"
},
{
"path": "../../remirror__extension-positioner/src"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/remirror__extension-entity-reference/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface EntityReferenceOptions {
*/
getStyle?: (entityReferences: EntityReferenceMetaData[][]) => Decoration[];
blockSeparator?: AcceptUndefined<string>;
onClickMark?: (id?: string) => void;
onClickMark?: (entityReferences?: EntityReferenceMetaData[]) => void;
}

export interface EntityReferencePluginState extends Required<EntityReferenceOptions> {
Expand Down
7 changes: 6 additions & 1 deletion support/root/.size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@
"name": "@remirror/extension-entity-reference",
"limit": "5 KB",
"path": "packages/remirror__extension-entity-reference/dist/remirror-extension-entity-reference.js",
"ignore": ["@remirror/pm", "@remirror/core", "@remirror/extension-positioner"],
"ignore": [
"@remirror/pm",
"@remirror/core",
"@remirror/extension-events",
"@remirror/extension-positioner"
],
"running": false,
"gzip": true
},
Expand Down

1 comment on commit f720dfe

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.