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

How to prevent the addition of marks on a Mention ? #1387

Closed
GitHubish opened this issue May 27, 2021 · 7 comments
Closed

How to prevent the addition of marks on a Mention ? #1387

GitHubish opened this issue May 27, 2021 · 7 comments
Labels
Type: Feature The issue or pullrequest is a new feature

Comments

@GitHubish
Copy link

I am trying to prevent the addition of marks (such as bold) on my mention type. I have extended the type that Tiptap offers by trying to specify :

marks: '',

addAttributes() {
    ...
}

2021-05-27_17-15-24

But it doesn't work. What am I missing?
Thanks

@GitHubish GitHubish added Type: Feature The issue or pullrequest is a new feature v2 labels May 27, 2021
@philippkuehn
Copy link
Contributor

It’s probably just a styling issue on your side. What’s the rendered markup?

@GitHubish
Copy link
Author

Here is the rendered markup and the JSON :

chrome_2021-05-27_17-54-06
chrome_2021-05-27_17-54-26

@philippkuehn
Copy link
Contributor

Since this is how HTML/CSS works you have to reset styling for your mentions.

.mention {
  font-weight: normal;
}

@GitHubish
Copy link
Author

@philippkuehn but isn't it possible that the JSON output does not include the marks on the Mention ? I have specified on the Mention extension that I do not allow marks with marks: ''. My problem is not graphic but in the model.

@philippkuehn
Copy link
Contributor

marks are controlled by the parent node. in your case the paragraph.

related: https://discuss.prosemirror.net/t/nodespec-marks-not-work-at-inline-node/2414

@GitHubish
Copy link
Author

Thank you for this clarification. I understand better why it didn't work. Is there a clean and efficient way to control this? Because more problematic, so it is possible to add a link on a content that has text and a Mention. While in my case the mention implements a click behavior to redirect to a page ? Thanks @philippkuehn !

@aliasliao
Copy link
Contributor

@GitHubish You can use appendTransaction to achieve this.

Here's the code I'm using to automatically clean up marks of mention elements:

import { Transaction, Plugin } from 'prosemirror-state';

function clearMentionMarks(
  tr: Transaction, // An old transaction
  newTr: Transaction, // The latest transaction
): boolean {
  let cleared = false;

  tr.doc.descendants((node, pos) => {
    if (node.type.name === 'mention' && node.marks.length > 0) {
      const mappedPos = newTr.mapping.map(pos);
      newTr.setNodeMarkup(mappedPos, node.type, node.attrs, []);
      cleared = true;
    }
  });

  return cleared;
}

/**
 * Remove marks from mention element.
 * */
export class ClearMentionMarksPlugin extends Plugin {
  constructor() {
    super({
      appendTransaction(transactions, oldState, newState) {
        // Create a new transaction.
        const newTr = newState.tr;

        let cleared = false;
        for (const transaction of transactions) {
          const anotherClear = clearMentionMarks(transaction, newTr);
          cleared = anotherClear || cleared;
        }
        if (cleared) {
          return newTr;
        }
      },
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature The issue or pullrequest is a new feature
Projects
None yet
Development

No branches or pull requests

3 participants