GithubHelp home page GithubHelp logo

Comments (12)

sytolk avatar sytolk commented on May 22, 2024 5

Works for React like that. Thanks @Saul-Mirone but this example miss me in docs

import { Slice } from "prosemirror-model";

const MilkdownEditor: React.FC = () => {
  const editorRef = useRef<EditorRef | null>(null);
  const [isContentLoaded, setContentLoaded] = useState<boolean>(false);

const editor = useEditor(
      (root) => {
        return new Editor()
            .config((ctx) => {
                ctx.set(rootCtx, root);
                ctx.set(defaultValueCtx, "Default value");
            })
            .use(commonmark)
            .use(clipboard)
            .use(listener)
            .use(history);
      },
      [],
  );

useEffect(() => {
   if (editorRef.current) {
      const editor = editorRef.current.get();
      if (editor) {
        editor.action((ctx: Ctx) => {
          const view = ctx.get(editorViewCtx);
          const parser = ctx.get(parserCtx);
          const doc = parser('Content changed !!!');
          if (!doc) return;
          const state = view.state;
          view.dispatch(
            state.tr.replace(
              0,
              state.doc.content.size,
              new Slice(doc.content, 0, 0)
            )
          );
        });
      }
    }
},[isContentLoaded])

  return <ReactEditor ref={editorRef} editor={editor} />;
};

export default MilkdownEditor;

from milkdown.

abhinandshibu avatar abhinandshibu commented on May 22, 2024 5

You can use the action feature, you can find example here, and here for a very simple vue example.

Unfortunately these links don't work anymore, is it possible for them to be updated?

For anyone else coming across this and trying to extract and set the state for react:

import { replaceAll, getMarkdown } from '@milkdown/utils';

const { get } = useEditor(...);

let editor = get();
editor?.action(replaceAll('# The markdown I am replacing it with'));
let myContent = editor?.action(getMarkdown());

Also this is useful:
https://milkdown.dev/docs/api/utils#macros

from milkdown.

Saul-Mirone avatar Saul-Mirone commented on May 22, 2024 1

You can use the action feature, you can find example here, and here for a very simple vue example.

from milkdown.

Saul-Mirone avatar Saul-Mirone commented on May 22, 2024 1

Let me add this in doc.

from milkdown.

Saul-Mirone avatar Saul-Mirone commented on May 22, 2024 1

I found a problem with this method. It doesn't preserve new lines like \n\n\n\n\n. I think remark removes them.

Is there a known solution?

That's how markdown syntax works and handled by remark, if you want to change this behavior, you need to write a remark plugin to do this.

from milkdown.

danieldaeschle avatar danieldaeschle commented on May 22, 2024

I found a problem with this method. It doesn't preserve new lines like \n\n\n\n\n. I think remark removes them.

Is there a known solution?

from milkdown.

sytolk avatar sytolk commented on May 22, 2024

@danieldaeschle its not a perfect solution (extra <div> is added) but you can try to create custom component for node: https://github.com/Saul-Mirone/milkdown/blob/main/gh-pages/pages/integrations/react/index.md#custom-component-for-node

with plugin is more complicated.. I have try to change onClick in link with a plugin without success: https://github.com/tagspaces/tagspaces-extensions/blob/Migrate-to-md-editor-extension-to-use-react/md-editor/src/App.tsx#L289

The problem is that plugin receive prosemirror nodes param and dont have access to DOM

from milkdown.

DogeVenci avatar DogeVenci commented on May 22, 2024

Is there an example of vue?

from milkdown.

Saul-Mirone avatar Saul-Mirone commented on May 22, 2024

Is there an example of vue?

Sure, here it is

https://codesandbox.io/s/milkdown-vue-update-ti4zb

from milkdown.

duhaime avatar duhaime commented on May 22, 2024

@sytolk in your example above where are editorViewCtx and parserCtx defined?

edit, oh:

import { editorViewCtx, parserCtx, serializerCtx } from '@milkdown/core';

from milkdown.

Saul-Mirone avatar Saul-Mirone commented on May 22, 2024

#288 Link for track

from milkdown.

solos avatar solos commented on May 22, 2024

Is there an example of angular?

from milkdown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.