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

Contenteditable support #310

Closed
Rich-Harris opened this issue Feb 28, 2017 · 4 comments · Fixed by #2379
Closed

Contenteditable support #310

Rich-Harris opened this issue Feb 28, 2017 · 4 comments · Fixed by #2379
Milestone

Comments

@Rich-Harris
Copy link
Member

Via #10 / #309. In Ractive this was done with...

<div contenteditable value='{{foo}}'></div>

...so the Svelte equivalent would be

<div contenteditable bind:value='foo'></div>

Arguably this should warn or throw if the element contains any children.

@Conduitry
Copy link
Member

It seems like this almost works already. If you change addElementBindings.js to use the input event instead of the change event in this case, then you can use bind:innerHTML='whatever' to do the two-way binding.

Is bind:value less confusing, even if it's less technically correct? We could put in special cases for contenteditable elements to always do the getting and setting with .innerHTML for contenteditable elements instead of .value.

@xori
Copy link

xori commented Sep 15, 2017

Personally I think bind:innerHTML='value' makes more sense. Divs don't have value properties after all. Vue opts for :text='val' and :html='val' which also make sense.

@nomve
Copy link

nomve commented Jun 24, 2018

I know it would be nice if it kind of worked out of the box, but this works too...

<h1 ref:title on:input="set({title: this.textContent})" contenteditable="true"></h1>

<script>
    export default {
        oncreate() {
            this.refs.title.textContent = this.get().title
        },
        data() {
            return {
                title: 'test'
            }
        }
    }
</script>

it just shows pesky warnings that h1 is empty... but even an empty space fixes that.

@leporo
Copy link
Contributor

leporo commented Apr 8, 2019

I've submitted a pull request implementing xori's suggestion for bind:text and bind:html. It basically maps :text to Element.textContent and :html to Element.innerHTML.

For non-contenteditable elements it throws a validation error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants