Skip to content

Commit

Permalink
feat(entity-reference): get references from arbitrary editor states (#…
Browse files Browse the repository at this point in the history
…1960)

* feat(entity-reference): add ability to get references from old editor states

* fix(react): expose `useDocChanged` hook
  • Loading branch information
whawker committed Dec 12, 2022
1 parent f51d293 commit 9778380
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-lions-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remirror/extension-entity-reference': minor
---

Entity references helpers can use other states than current state
5 changes: 5 additions & 0 deletions .changeset/curly-monkeys-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remirror/react-core': patch
---

Expose `useDocChanged` hook
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@ function create(options: EntityReferenceOptions = {}) {
return renderEditor([entityReferenceExtension]);
}

let editor = create();
let {
add,
nodes: { doc, p },
selectText,
insertText,
commands,
helpers,
view,
} = create();
} = editor;

beforeEach(() => {
editor = create();
({
add,
nodes: { doc, p },
selectText,
insertText,
helpers,
commands,
view,
} = create());
} = editor);
});

describe('schema', () => {
Expand Down Expand Up @@ -222,6 +226,26 @@ describe('EntityReference marks', () => {
const entityReferences = helpers.getEntityReferencesAt(2);
expect(entityReferences).toHaveLength(2);
});

it('returns entityReferences for the given state', () => {
add(doc(p('Some text')));

selectText({ from: 3, to: 5 });
commands.addEntityReference();
selectText({ from: 3, to: 10 });
commands.addEntityReference();

const prevState = editor.state;

selectText(1);
insertText('Inserted ');

const currentEntityReferences = helpers.getEntityReferencesAt(3);
expect(currentEntityReferences).toHaveLength(0);

const prevEntityReferences = helpers.getEntityReferencesAt(3, prevState);
expect(prevEntityReferences).toHaveLength(2);
});
});

describe('getEntityReferenceId', () => {
Expand All @@ -239,6 +263,31 @@ describe('EntityReference marks', () => {

expect(entityReferenceFromDoc).toEqual(entityReference);
});

it('returns entityReference by Id for the given state', () => {
add(doc(p('testing text')));
const entityReference = {
id: 'testId',
from: 9,
to: 13,
text: 'text',
};
selectText({ from: entityReference.from, to: entityReference.to });
commands.addEntityReference(entityReference.id);

const prevState = editor.state;

commands.removeEntityReference(entityReference.id);

const entityReferenceFromDoc = helpers.getEntityReferenceById(entityReference.id);
expect(entityReferenceFromDoc).toBeUndefined();

const prevEntityReferenceFromDoc = helpers.getEntityReferenceById(
entityReference.id,
prevState,
);
expect(prevEntityReferenceFromDoc).toEqual(entityReference);
});
});

describe('scrollIntoEntityReference', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,12 @@ export class EntityReferenceExtension extends MarkExtension<EntityReferenceOptio
* `joinDisjoinedEntityReferences`.
*/
@helper()
getEntityReferences(): Helper<EntityReferenceMetaData[]> {
getEntityReferences(
state: EditorState = this.store.getState(),
): Helper<EntityReferenceMetaData[]> {
const entityReferences = getEntityReferencesFromPluginState({
extension: this,
state: this.store.getState(),
state,
}).flat();
return joinDisjoinedEntityReferences(entityReferences);
}
Expand All @@ -284,10 +286,13 @@ export class EntityReferenceExtension extends MarkExtension<EntityReferenceOptio
*
*/
@helper()
getEntityReferenceById(entityReferenceId: string): Helper<EntityReferenceMetaData | undefined> {
getEntityReferenceById(
entityReferenceId: string,
state: EditorState = this.store.getState(),
): Helper<EntityReferenceMetaData | undefined> {
const entityReferences = getEntityReferencesFromPluginState({
extension: this,
state: this.store.getState(),
state,
}).flat();
return joinDisjoinedEntityReferences(entityReferences).find(
(entityReference) => entityReference.id === entityReferenceId,
Expand All @@ -301,8 +306,10 @@ export class EntityReferenceExtension extends MarkExtension<EntityReferenceOptio
*
*/
@helper()
getEntityReferencesAt(pos?: PrimitiveSelection): Helper<EntityReferenceMetaData[]> {
const state = this.store.getState();
getEntityReferencesAt(
pos?: PrimitiveSelection,
state: EditorState = this.store.getState(),
): Helper<EntityReferenceMetaData[]> {
const { doc, selection } = state;
const { from, to } = getTextSelection(pos ?? selection, doc);

Expand Down
1 change: 1 addition & 0 deletions packages/remirror__react-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
useChainedCommands,
useCommands,
useCurrentSelection,
useDocChanged,
useEditorDomRef,
useEditorState,
useEditorView,
Expand Down

1 comment on commit 9778380

@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.

🎉 Published on https://remirror.io as production
🚀 Deployed on https://63970c0afc5aab761a0f0838--remirror.netlify.app

Please sign in to comment.