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

suneditor.insertHTML don't work #970

Closed
leandrocombr opened this issue Mar 30, 2022 · 2 comments
Closed

suneditor.insertHTML don't work #970

leandrocombr opened this issue Mar 30, 2022 · 2 comments

Comments

@leandrocombr
Copy link

I'm trying to use the function: insertHTML

As per manual: editor.insertHTML('', true, true);

I created a button and put a jquery

suneditor.insertHTML('Text 1', true, true);

But when clicking the button it does not add the text. But if I click on the editor and then click on the button, the text is added.

What am I doing wrong?

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/3.0.1/github-markdown.min.css">
<link href="http://suneditor.com/dist/css/suneditor.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codemirror@5.49.0/lib/codemirror.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>


<button id="text1">Text 1</button>
<button id="text2">Text 2</button>
<button id="text3">Text 3</button>



<textarea id="exampleOptions" style="width:100%; height:200px;">
Teste de Editor
</textarea>

<script src="http://suneditor.com/sample/js/common.js"></script>
<script src="http://suneditor.com/dist/suneditor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/codemirror@5.49.0/lib/codemirror.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/codemirror@5.49.0/mode/htmlmixed/htmlmixed.js"></script>
<script src="https://cdn.jsdelivr.net/npm/codemirror@5.49.0/mode/xml/xml.js"></script>
<script src="https://cdn.jsdelivr.net/npm/codemirror@5.49.0/mode/css/css.js"></script>
<script src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js"></script>
<script>
$(document).ready(function(){

	$("#text1").click( function(){
		suneditor.insertHTML('Text 1', true, true);
	});	
	$("#text2").click( function(){
		suneditor.insertHTML('Text 3', true, true);
	});	
	$("#text3").click( function(){
		suneditor.insertHTML('Text 2', true, true);
	});	

    suneditor = SUNEDITOR.create('exampleOptions',{
        font : [
            'Arial',
            'tahoma',
            'Courier New,Courier'
        ],
        fontSize : [
            8, 10, 14, 18, 24, 36
        ],
        colorList: [
            ['#ccc', '#dedede', 'OrangeRed', 'Orange', 'RoyalBlue', 'SaddleBrown'],
            ['SlateGray', 'BurlyWood', 'DeepPink', 'FireBrick', 'Gold', 'SeaGreen']
        ],
        paragraphStyles: [
            'spaced',
            'neon',
            {
                name: 'Custom',
                class: '__se__customClass'
            }
        ],
        textStyles: [
            'translucent',
            {
                name: 'Emphasis',
                style: '-webkit-text-emphasis: filled;',
                tag: 'span'
            }
        ],
        width: '100%',
        maxWidth: '600px',
        minWidth: 400,
        height : 'auto',
        videoWidth : '80%',
        youtubeQuery : 'autoplay=1&mute=1&enablejsapi=1',
        imageWidth : 150,
        popupDisplay: 'local',
        resizingBar: false,
        buttonList : [
            ['font', 'fontSize',
            'fontColor', 'hiliteColor', 'video',
            'paragraphStyle', 'textStyle',
            'fullScreen', 'codeView']
        ],
        callBackSave: function (contents) {
            alert(contents);
        }
    });
});
</script>
@AliBayatMokhtari
Copy link

AliBayatMokhtari commented Apr 1, 2022

I believe that insertHTML is working with the last cursor position. So if you don't focus the editor, last position of cursor is undefined and insertHTML will not work until you focus the editor. By the way i believe there is focus method that you can focus the editor then use insertHTML method. Docs

@JiHong88 JiHong88 added this to the 2.43.0 milestone Apr 2, 2022
JiHong88 added a commit that referenced this issue Apr 3, 2022
@JiHong88 JiHong88 closed this as completed Apr 3, 2022
@JiHong88
Copy link
Owner

The 2.43.0 version has been updated.
If this issue has not been resolved, please reopen this issue.
Thank you.

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

No branches or pull requests

3 participants