question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I update attributes for a Mark?

See original GitHub issue

Hi - I am trying to create a text highlight feature where a user can select some text and add a highlight color to it.

I created a custom mark as follows -

export default class annotation extends Mark 
{
    get name() 
    {
        return 'annotation'
    }

    get schema() 
    {
        return {
            attrs: {
                "highlight-color" : {
                    default: "default"
                },
            },
            // inclusive: false,
            parseDOM: [
                {
                    tag: 'annotation',
                    getAttrs: dom => ({
                        "highlight-color" :dom.getAttribute('highlight-color'),
                    }),
                },
            ],
            toDOM: node => ['annotation', {
                "highlight-color" : node.attrs["highlight-color"]
            }, 0],
        }
    }
    commands({ type, schema }) 
    {
        return attrs => 
        {
            return updateMark(type, attrs);
        }
    }
    get view() 
    {
        return hightlight;
    }

I created a custom Vue component to render it

<template>
    <span class="highlight" @click="changeColor()">
        <span :class="[colorClass]"
        >
            {{ node.textContent }}
        </span>
    </span>
</template>

<script>
export default {
    name: "Hightlight",
    props: ['node', 'updateAttrs', 'editable', 'options', 'view', 'getPos'],
    computed: 
    {
        colorClass : function()
        {
            return this.node.attrs["highlight-color"];
        }

    },
    mounted()
    {
        console.log(this.node);
    },
    methods : {
        changeColor : function()
        {
            console.log("Change Color");
            let newClass = "yellow"
            
            **HOW DO I SET THIS NEW CLASS ON THE MARK??**
        }
    }
}
</script>

I’m trying to add a new class to the mark based on user interaction. How do I update the color class? I tried using the “updateAttrs” prop but I keep getting “this.getPos is not a function”.

I am a bit lost so any help/direction would be really helpful.

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
philippkuehncommented, May 15, 2019

I’ve released a new version with a fix for getPos(). You should now be able to call updateAttrs() for marks. Currently this is limited to the parent node of a mark because of the current implementation of getMarkRange. https://github.com/scrumpy/tiptap/blob/master/packages/tiptap-utils/src/utils/getMarkRange.js

But it should be absolutely possible to extend getMarkRange to lookup in its siblings to improve it even further. Maybe someone will create a PR for that 😅

2reactions
kshitizshankarcommented, May 15, 2019

Got it working with Marks for now with custom component. Am storing only the highlight-id in the attributes which I don’t really need to update. I am querying my API with the ID to render additional details like color etc. (Storing them outside of the document made the most sense for my use case).

The approach for creating slices and replacing data ended up causing a lot more issues, and marks seem the right way to go for this.

Thanks a lot for all the help!


In case it helps anyone else - here is the code I’m using for now -

export default class highlight extends Mark 
{
    get name() 
    {
        return 'highlight'
    }

    get schema() 
    {
        return {
            attrs: {
                "highlight-id": {
                    default: null
                }
            },
            inclusive: true,
            parseDOM: [
                {
                    tag: 'highlight',
                    getAttrs: dom => ({
                        "highlight-id": dom.getAttribute('highlight-id'),
                    }),
                },
            ],
            toDOM: mark => ['highlight', {
                "highlight-id": mark.attrs["highlight-id"],
            }, 0],
        }
    }

    commands({ type, schema }) 
    {
        return attrs => 
        {
            let newHighlightId;
            if(attrs && attrs["highlight-id"])
            {
                newHighlightId = attrs["highlight-id"];
            }
            else
            {
                newHighlightId = parseInt(Math.random()*10000000);
            }
            return updateMark(type, {"highlight-id" : newHighlightId});
        }
    }

    get view() 
    {
        return HightlightView;
    }
}

HighlightView.vue

<template>
    <span class="highlight" @click="openDetails()">
        <span ref="content"
              class=""
              :highlight-id="idhighlight"
        /></span>
</template>
export default {
    name: "HighlightView",
    props: ['node', 'updateAttrs', 'editable'],
    data() 
    {
        return {
        }
    },
    computed: 
    {
        idhighlight: {
            get() 
            {
                return this.node.attrs["highlight-id"];
            },
        },
    },
    methods : 
    {
        openDetails : function()
        {
            //Do Stuff
        }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating mark attributes - Discuss - ProseMirror
I'm making a storytelling application that uses ProseMirror with a custom mark. This mark has one attribute, targetCharacters, ...
Read more >
updateAttributes – Tiptap Editor
The updateAttributes command sets attributes of a node or mark to new values. Not passed attributes won't be touched.
Read more >
How to Update Attributes through Import
4. Mark Update Existing Records, Validate Only, and select Attribute Import ID for the Use field. User-added image. 5. Select the File Layout...
Read more >
Solved: How to Update attributes in an Online Feature Laye...
I have a large feature layer hosted in ArcGIS Online. I want to select a subset by matching a field attribute to a...
Read more >
The attribute doesn't show up in the “Update Attributes” screen
To use this feature, you need to go to the products grid, mark the products that you want to update and click on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found