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.

[QUESTION] Custom component - how to set editable to true

See original GitHub issue

Hi, I’ve created a custom component type, but I am having some trouble making it editable. The component is a “span” with component type “tag”. it will contain only text, but I want it to extend the default, not the text component. Any idea how can I make it editable, even if it is not “text”.

comps.addType('tag', {
        
        model: { 
            defaults:  {
                type: 'tag',
                tagName: 'span',
                editable: true,
                droppable: false,
                attributes: {
                  'data-gjs-type': 'tag',
                },
                textable: 1,
                traits: [{
                    type: 'select',
                    label: 'Tag',
                    name: 'selected-tag',
                    options: tagTypes,
                }], 
            },

            isComponent: function(el) {
              let result;
              if (el.tagName == 'SPAN' && el.getAttribute("data-gjs-type") === "tag") {
                result = {
                  type: 'tag',
                };
              }
              return result;
            }
        }, ... 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
inaLarcommented, Aug 8, 2019

Thank you! I have done something similar, but yours is not so complicated. I’ve had other events attached to onActive, so I couldn’t use it. Now I detached them and I will do as you suggest. About the data-gjs-type - I need it because the content I am creating is stored and when reloaded the components have to be recognized in order for their settings/traits to be loaded and especially this custom type component I’ve added. There is also a possibility to change the content via traits, which complicates things a bit too 😃 P.S.: @artf You have done a great work by far!

0reactions
artfcommented, Aug 6, 2019

I think you’re looking for something like this

editor.DomComponents.addType('tag', {
	model: {
		defaults: {
                tagName: 'span',
                editable: true,
                droppable: false,
				// ... all your other stuff
		}
	},
	view: {
		events: {
			dblclick: 'onActive',
			focusout: 'onDisable',
		},
		onActive() {
			this.el.contentEditable = true;
		},
		onDisable() {
			const { el, model } = this;
			el.contentEditable = false;
			model.set('content', el.innerHTML)
		},
	}
});

ps: there is no need to check/handle for data-gjs-type, the editor does it already for you. If you don’t import HTML and need to recognize your tag custom component you can skip isComponent

Read more comments on GitHub >

github_iconTop Results From Across the Web

React custom component containing editable ...
We are using custom React components and they are working well. However, I would like to implement a custom component that behaves like...
Read more >
How do I add the ability to edit text within a react component?
Your Task component needs a keyPress handler to set isEditing to false when enter is pressed: const handleKeyPress = (e) => { if...
Read more >
contenteditable - HTML: HyperText Markup Language | MDN
Chrome Edge contenteditable Full support. ChromeYes. Toggle history Full suppo... contenteditable="caret". Experimental Full support. ChromeYes. Toggle history Full suppo... contenteditable="events". Experimental Full support. ChromeYes. Toggle history...
Read more >
Create Editable HTML in React
There is an HTML attribute you can pass to any HTML tags which makes it editable. If you set contenteditable=true for nearly any...
Read more >
Make a Custom Data Type Editable
Accessibility of Editable Custom Types · Add the attribute data-inputable="true" to the input component in the edit template. · Set standardCellLayout: true for ......
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