Skip to content

Commit

Permalink
chore: Minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsivin committed Sep 3, 2024
1 parent ded986c commit 2241b72
Showing 1 changed file with 73 additions and 71 deletions.
144 changes: 73 additions & 71 deletions src/mentions/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,94 +53,96 @@ export const suggestionsPlugin = ({
onChange = () => false,
onExit = () => false,
onKeyDown = () => false,
}) => new Plugin({
key: new PluginKey('mentions'),
}) => {
return new Plugin({
key: new PluginKey('mentions'),

view() {
return {
update: (view, prevState) => {
const prev = this.key.getState(prevState);
const next = this.key.getState(view.state);
view() {
return {
update: (view, prevState) => {
const prev = this.key.getState(prevState);
const next = this.key.getState(view.state);

const moved =
const moved =
prev.active && next.active && prev.range.from !== next.range.from;
const started = !prev.active && next.active;
const stopped = prev.active && !next.active;
const changed = !started && !stopped && prev.text !== next.text;

if (stopped || moved)
onExit({ view, range: prev.range, text: prev.text });
if (changed && !moved)
onChange({ view, range: next.range, text: next.text });
if (started || moved)
onEnter({ view, range: next.range, text: next.text });
},
};
},

state: {
init() {
return {
active: false,
range: {},
text: null,
const started = !prev.active && next.active;
const stopped = prev.active && !next.active;
const changed = !started && !stopped && prev.text !== next.text;

if (stopped || moved)
onExit({ view, range: prev.range, text: prev.text });
if (changed && !moved)
onChange({ view, range: next.range, text: next.text });
if (started || moved)
onEnter({ view, range: next.range, text: next.text });
},
};
},

apply(tr, prev) {
const { selection } = tr;
const next = { ...prev };

if (selection.from === selection.to) {
if (
selection.from < prev.range.from ||
selection.from > prev.range.to
) {
next.active = false;
}
state: {
init() {
return {
active: false,
range: {},
text: null,
};
},

const $position = selection.$from;
const match = matcher($position);
apply(tr, prev) {
const { selection } = tr;
const next = { ...prev };

if (match) {
next.active = true;
next.range = match.range;
next.text = match.text;
if (selection.from === selection.to) {
if (
selection.from < prev.range.from ||
selection.from > prev.range.to
) {
next.active = false;
}

const $position = selection.$from;
const match = matcher($position);

if (match) {
next.active = true;
next.range = match.range;
next.text = match.text;
} else {
next.active = false;
}
} else {
next.active = false;
}
} else {
next.active = false;
}

if (!next.active) {
next.range = {};
next.text = null;
}
if (!next.active) {
next.range = {};
next.text = null;
}

return next;
return next;
},
},
},

props: {
handleKeyDown(view, event) {
const { active } = this.getState(view.state);
props: {
handleKeyDown(view, event) {
const { active } = this.getState(view.state);

if (!active) return false;
if (!active) return false;

return onKeyDown({ view, event });
},
decorations(editorState) {
const { active, range } = this.getState(editorState);
return onKeyDown({ view, event });
},
decorations(editorState) {
const { active, range } = this.getState(editorState);

if (!active) return null;
if (!active) return null;

return DecorationSet.create(editorState.doc, [
Decoration.inline(range.from, range.to, {
nodeName: 'span',
class: suggestionClass,
}),
]);
return DecorationSet.create(editorState.doc, [
Decoration.inline(range.from, range.to, {
nodeName: 'span',
class: suggestionClass,
}),
]);
},
},
},
});
});
};

0 comments on commit 2241b72

Please sign in to comment.