Skip to content

Commit

Permalink
e
Browse files Browse the repository at this point in the history
  • Loading branch information
winetree94 committed Dec 22, 2023
1 parent 0efffbb commit 6d53198
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/docs/docs/packages/mention.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ npm install @edim-editor/mention

#### `edimMentionUnsetPlugins`

멘션 서식의 일부 텍스트가 지워진 경우 멘션을 해제하는 기능을 제공합니다.
멘션 서식이 적용된 텍스트가 일부 지워진 경우 멘션을 해제하는 기능을 제공합니다.

## 사용 예시

Expand Down
4 changes: 4 additions & 0 deletions packages/mention/src/commands/add-mention.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Command } from 'prosemirror-state';

/**
* To initiate a mention input, insert the @ character.
* If there is no space before the current cursor position, it will be inserted along with a space.
*/
export const addMention = (): Command => {
return (state, dispatch) => {
let tr = state.tr;
Expand Down
3 changes: 3 additions & 0 deletions packages/mention/src/plugins/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export interface EdimMentionPluginConfigs {
) => MentionPluginView;
}

/**
* Provides a feature to display a mention search popup when the "@" character is entered.
*/
export const edimMentionCommandPlugins = (
configs: EdimMentionPluginConfigs,
) => {
Expand Down
74 changes: 38 additions & 36 deletions packages/mention/src/plugins/unset.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
import { Plugin } from 'prosemirror-state';

/**
* Provides a feature to unmention when text with mention formatting is partially deleted.
*/
export const edimMentionUnsetPlugins = () => {
const mentionPlugin: Plugin =
new Plugin({
appendTransaction: (transactions, oldState, newState) => {
const tr = newState.tr;
let modified = false;
const mentionPlugin: Plugin = new Plugin({
appendTransaction: (transactions, oldState, newState) => {
const tr = newState.tr;
let modified = false;

if (!transactions.some((transaction) => transaction.docChanged)) {
if (!transactions.some((transaction) => transaction.docChanged)) {
return;
}

newState.doc.descendants((node, pos) => {
const hasMention = node.marks.some(
(mark) => mark.type.name === 'mention',
);
if (!node.isText || !hasMention) {
return;
}

newState.doc.descendants((node, pos) => {
const hasMention = node.marks.some(
try {
const oldNode = oldState.doc.nodeAt(pos);
const oldNodeHasMention = oldNode?.marks.some(
(mark) => mark.type.name === 'mention',
);
if (!node.isText || !hasMention) {
return;
}
try {
const oldNode = oldState.doc.nodeAt(pos);
const oldNodeHasMention = oldNode?.marks.some(
(mark) => mark.type.name === 'mention',
);

if (
oldNode &&
oldNodeHasMention &&
oldNode.isText &&
node.text !== oldNode.text
) {
tr.removeMark(
pos,
pos + node.nodeSize,
newState.schema.marks['mention'],
);
modified = true;
}
} catch (error) {
console.warn('mention not found');
if (
oldNode &&
oldNodeHasMention &&
oldNode.isText &&
node.text !== oldNode.text
) {
tr.removeMark(
pos,
pos + node.nodeSize,
newState.schema.marks['mention'],
);
modified = true;
}
});
} catch (error) {
console.warn('mention not found');
}
});

return modified ? tr : null;
},
});
return modified ? tr : null;
},
});

return [mentionPlugin];
};

0 comments on commit 6d53198

Please sign in to comment.