sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
77.74k stars 4.05k forks source link

[Feature] Bind to comment & text nodes with `svelte:element` #7424

Open AlbertMarashi opened 2 years ago

AlbertMarashi commented 2 years ago

Describe the problem

Having the ability to bind to text or comment nodes would be valuable for people developing CMS tools, page builders and other content-editing related projects.

Related issues:

https://github.com/sveltejs/svelte/issues/4544

Describe the proposed solution

Ability to bind to a special svelte element type such as <svelte:text> or <svelte:comment> or the proposed <svelte:element tag="#text">

Text Nodes are a valid Node in HTML, and come with all the properties and methods that people will find valuable to be able to bind to, such as .after() .nextSibling .parentNode and so on.

Editors like Prose mirror attach metadata to nodes in order to count cursor offsets and lengths, and being able to do this would be valuable.

Alternatives considered

Creating a useless element and binding to that in order to get to the text node, eg:

<script>
  let ref;

  onMount(() => {
    const child = ref.nextElementSibling;
  });
</script>
<span bind:this={ref} style="display:none"></span>
<slot>

Importance

AlbertMarashi commented 9 months ago

bump

AlbertMarashi commented 8 months ago

@dummdidumm could we get a look at implementing this for 5.0?

ie <svelte:node type="comment|text" value="...">

Rich-Harris commented 3 months ago

This seems super niche, and solvable in userland already. Can you elaborate on why this would be useful?

AlbertMarashi commented 3 months ago

@Rich-Harris it's extremely useful when building contenteditable editors, in order to attach various properties to the node for tracking. This could be achieved with either a #comment or #text node.

When solved in userland, it involves wrapping the text in an element in order to get access to the text Text Node.

However, I will close as I no longer need this as I am not working on that project anymore

AlbertMarashi commented 1 month ago

I need something like this again