BearToCode / carta

A lightweight, fast and extensible Svelte Markdown editor and viewer.
https://beartocode.github.io/carta/
MIT License
264 stars 10 forks source link

Lib Version for native html #37

Open pnegahdar opened 4 months ago

pnegahdar commented 4 months ago

This editor is so cool, kudos!

I'm not too familiar with svelte, but how hard would it be to compile this into JS and have a pure JS api to bind it to an element.

carta = new Cart()
render(carta, document.getElementById("editor")) 

That way it can be used in a react or vanilla project as well?

BearToCode commented 4 months ago

Hi @pnegahdar! It would be really cool to be able to use the editor with other frameworks such as React and Vue. Honestly, I don't have experience with compiling Svelte and using in other frameworks, but quickly looking online seems tricky but possible. For example, I saw that bytemd, another popular markdown editor, already does so.

I currently do not have time to dedicate to this, but I'll leave the issue open: I may come back to this in the future, or maybe you or some other developer may help 😄

mithi commented 1 week ago

Hi @BearToCode ,

I know that currently you don't have time to dedicate to this,

I was just wondering if it is possible to sponsor you financially or for you to take a commission (Not soon maybe 1 to 2 months from now when Svelte 5 is stable?) to do the following:

  1. to create a new version that ports this to Svelte 5 (including plugins, use Runes and remove deprecated features that will be removed in Svelte 6)

  2. to do some research and write from guiding instructions on how to theoretically use the Carta object and build our the vanilla js components for the following:

I'm imagining it will be something like quill mention or Niivue

Where we can create our own components like (Just throwing initial ideas around)

const carta = new Carta({ extensions: [hashtag()] });

const markdownEditor = new MarkdownEditor(
  { carta,
    debounce: 500,
    onDebouncedChange: (insertedTextSinceLastCalled: string, currentPosition: number) => {
      // maybe get the AST here
      console.log(carta.currentASTRepresentation);
      console.log(carta.currentStringRepresentation);
      console.log(insertedTextSinceLastCalled, currentPosition)
      // so we can save it on our server
      // or plug it in on our own previewer 
    },
    onDispatchEvent: (eventType, payload) => {
      // maybe do something given eventType and payload
      console.log(eventType === "carta-render", payload) // onKeyUp, onKeyDown, onBlur
      console.log(eventType === "onClick", payload.cursorPosition) 
    }
  }).

 markdownEditor.attachToId("carta-markdown-editor-container")

<div id="carta-markdown-editor-container" />

I know these tasks are quite involved, but I think you have done incredible work..

BearToCode commented 1 week ago

Hi @mithi,

I appreciate very much your comment 😄 . I don't think I'll be able to work on this for at least a couple of months, due to my enrollment in university, but I should be able to do something then.

I think you asked for two very different modifications in your comment in terms of work required: migrating to Svelte 5 and creating a vanilla JS version of the library.

For the first one, even thought Svelte 5 is not yet definitive and I've not worked directly with it, I'm quite confident not too much work will be needed. This project does not make extensive use of Svelte components, so migrating shoud be straightforward. I'd still wait for a stable release before doing so(as you said).

Providing a vanilla version of the library is a lot more tricky and requires planning. There are several points that need to be adressed to make it viable, as Svelte(Kit) does not provide a simple universal way to do so:

There are probably other things that I forgot/will discover during developing.

Regarding the commission: I've created this project because I needed an editor myself, and I decided to share it. I don't see developing OSS as a job. For this reason, I don't quite like the idea of a commision. Still, if you find this project valuable and plan to make a donation, based on how much value it has provided you, I'll highly appreciate it ❤️ . If you need the previously mentioned support to be able to use the editor, maybe wait until it has been implemented. I might look into setting up a GitHub sponsor page.

If in the meantime I hopefully get time to dedicate to this, you'd like to start working on it yourself, start making some other changes or have any question regarding the project, feel free to ask.

mithi commented 1 week ago

Hi @BearToCode

I really appreciate your detailed response to my query.

I'm sorry I think I did not word the second request properly, I didn't mean to ask you do create a vanilla JS version of the library, that would be too much work for you!

It's more like I noticed that the Carta class is purely written in Typescript and I think that it would be possible for for me to use that Carta instance and build my own Markdown editor on top of it instead (can be in Svelte 5 or VueJS or React).

I was just hoping I could request to you to write more detailed documentation on how the svelte 4 component MarkdownEditor (and other surrounding components like Input) uses the Carta instance under the hood...

I believe that a documentation will help provide some direction and guidance so I can write my own MarkdownEditor in other frameworks - a MarkdownEditor Component (in React and/or Vue) that uses the Carta instance you have written.

As you've mentioned that this library does not make extensive use of Svelte components, so I was thinking that maybe I can actually migrate it myself, if you don't have time to do so. Svelte 5 is now on Release Candidate phase (https://svelte.dev/blog/svelte-5-release-candidate)

I quote from there blog post:

It doesn't mean that it's ready for production, or that nothing will change between now and 5.0. But if you've held off on dabbling with Svelte 5 during the public beta phase, now is a great time to try it out...

I understand that you don't quite like the idea of commission, I'd gladly make a donation (Maybe through PayPal or Buy me a Coffee (Like https://ko-fi.com/minimithi) as an appreciation of your time and effort into writing this.

I tried to read your MarkdownEditor component (and the other components surrounding it like the Input and Renderer) but it's quite difficult for me to grasp (partially because I haven't played around with Svelte 4 as much as I should), and also unfortunately I'm also a bit overwhelmed since I have very little understanding on the system design of this library, and the reasonings behind design decisions.

I can try to spend a bit more time to try to explore and play around but then I thought maybe it's more efficient to just ask you directly for help since you know this library inside out (naturally because you wrote it!)

TLDR

I was wondering if you can I can ask you to write more detailed documentation on how the Svelte 4 components you wrote utilizes the Carta object under the hood so I can build my custom markdown editor in other frameworks (and in Svelte 5) to use the Carta object. The documentation would also help me migrate your existing components (including some of your plugins like the Slash one), from Svelte 4 to Svelte 5, if you don't have time to do so. I understand that you don't quite like the idea of commission, I'd gladly make a donation as an appreciation of your time and effort into writing this.

Again, thank you so much for creating this library and making it open source... It's the most awesome extendable markdown editor I've seen ever! 🥳 🥳 👏 👏

BearToCode commented 1 week ago

Hi again,

Using the Carta class to build "components" for a vanilla version or other framerworks seems like a great idea! It probably has some limitations, but overall it is much easier and more robust than just compiling the components.

If you just need me to explain the roles of the components and how they work, I can do that easily. I think I could add an header to the various components explaining them, and add descriptions to their functions and properties:

<!-- 
    @description A description of the what the component does and how it does that
-->
<script lang="ts">
  /**
   * A description of the property.
   */
  export let prop: Something;

  /**
   * What the function does
   * @param a What is a
   * @param b What is b
   */
  function fn(a, b) {

  }
</script>

Let me know if something like this would be enough, or if you had another idea in mind. You could obviously contact for any question.

Following your suggestion I created a ko-fi page.

mithi commented 1 week ago

I think I could add an header to the various components explaining them, and add descriptions to their functions and properties

Yes that would be really cool! But if you're doing this just for me, and if it will take too much time from more important things, then please don't prioritize this! After familiarizing myself with Svelte more the past few hours I think I can read and understand the code a bit better and I'll message you if I have any questions or clarifications.

I'm hoping to contribute more to this project in the near future when I'm no longer swamped with work... So no hurry really...

Following your suggestion I created a ko-fi page.

I have made a little donation on your Ko-Fi page, and I'll be making more donations in the future once I get to spend more time playing with this library.

Again, thank you so much for entertaining all my questions! 🙂

I will message you again in the next couple of months 🙂

BearToCode commented 1 week ago

Thanks a lot for your donation! Looking forward to working with you 😄