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

Unable to append child into editor div #1637

Closed
ct1735x opened this issue Jul 5, 2021 · 12 comments
Closed

Unable to append child into editor div #1637

ct1735x opened this issue Jul 5, 2021 · 12 comments
Labels
Enhancement Enhance performance or improve usability of original features.

Comments

@ct1735x
Copy link

ct1735x commented Jul 5, 2021

I want to insert a div, p or a in the editor with appendChild but it is not working. I can appendChild on any element but not in the editor div. (# prosemirror)

let fileLink = document.createElement('p');
fileLink.textContent = "hello !";
document.getElementById('ProseMirrorId').appendChild(fileLink);

Nothing happens.

last code line works in browser console without appendChild, which means the selector is ok.

I'm in the wysiwyg view

editor.insertText works, I wonder if you could add another function like editor.insertElement or insertNode

@ct1735x ct1735x added the Bug label Jul 5, 2021
@z-950
Copy link
Contributor

z-950 commented Jul 6, 2021

I think #1621 can help you, though it is hack.

@ct1735x
Copy link
Author

ct1735x commented Jul 6, 2021

How do I use this ? Do I have to write all of this everytime I want to insert an element ? Or I can use it like a function

@z-950
Copy link
Contributor

z-950 commented Jul 6, 2021

This function can help you to create element. And then insert it.

Just like this:

editor.insertHtml = (type, attrs, content) => {
    const wwSelection = editor.getSelection();
    const node = createNode(type, attrs, content);
    const newDoc = editor.wwEditor.schema.nodes.doc.create(null, node);

    const { tr } = editor.wwEditor.view.state;

    editor.wwEditor.view.dispatch(tr.replaceWith(wwSelection[0], wwSelection[1], newDoc));
};

@ct1735x
Copy link
Author

ct1735x commented Jul 6, 2021

Thanks! But I'm using CDN, no imports. WWE is unknown

@z-950
Copy link
Contributor

z-950 commented Jul 6, 2021

Thanks! But I'm using CDN, no imports. WWE is unknown

editor.wwEditor is wwe

@ct1735x
Copy link
Author

ct1735x commented Jul 6, 2021

I figured out how to do it, thanks for your help

@js87zz
Copy link
Contributor

js87zz commented Jul 6, 2021

@z-950 @2klm
As you said, there is no API to insert html nodes .
It's not as good to have direct access to the internal editor code as you do now.
So we'll provide API or command later.

@js87zz js87zz added Enhancement Enhance performance or improve usability of original features. and removed Bug labels Jul 6, 2021
@ct1735x
Copy link
Author

ct1735x commented Jul 6, 2021

@z-950 @js87zz do you know how do I use this to create a 'a href ' element ? There is no 'link' object in the nodes list
I tried this : editor.insertHtml('link', null, createText('test'));
but 'link' doesnt exist. Availabe objects are :
{ doc: {…}, paragraph: {…}, text: {…}, heading: {…}, codeBlock: {…}, bulletList: {…}, orderedList: {…}, listItem: {…}, blockQuote: {…}, table: {…}, … } and a few more but no link

however, I can see in the editor config : node = createNode('link', this.sourcepos(startpos, this.pos));
which makes me think it is possible to create a link node.

I can't wait for an API or a command to be released since I'm working on a project for a customer and I have a short schedule

@ct1735x
Copy link
Author

ct1735x commented Jul 6, 2021

I tried to add this in custom html renderers :

htmlBlock: {
        a(node) {
            return [
                { type: 'openTag', tagName: 'a', outerNewLine: true, attributes: node.attrs },
                { type: 'html', content: linkText },
                { type: 'closeTag', tagName: 'a', outerNewLine: true },
            ];
        },
    }

which works, but node.attrs doesnt so I cant add the " href='..' " part

Does it work like so ? editor.insertHtml('a', {linkUrl: 'https://google.com'} );

@z-950
Copy link
Contributor

z-950 commented Jul 7, 2021

@2klm
link or <a> is htmlInline, should use editor.wwEditor.schema.marks.doc.create(...), accorading to this

@js87zz
Copy link
Contributor

js87zz commented Jul 7, 2021

@2klm
I don't think it's necessary to complicate that much if it's simply inserting a link node.
It doesn't seem to need a custom renderer either.
You can insert the link node through executing the command as below.

const attrs = {
  linkUrl: // ...
  linkText: // ...
};
editor.exec('addLink', attrs);

@ct1735x
Copy link
Author

ct1735x commented Jul 7, 2021

@2klm
I don't think it's necessary to complicate that much if it's simply inserting a link node.
It doesn't seem to need a custom renderer either.
You can insert the link node through executing the command as below.

const attrs = {
  linkUrl: // ...
  linkText: // ...
};
editor.exec('addLink', attrs);

Thanks! I didnt know about that command stuff. Is there a doc about commands ?

@ct1735x ct1735x closed this as completed Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Enhance performance or improve usability of original features.
Projects
None yet
Development

No branches or pull requests

3 participants