JiHong88 / suneditor

Pure javascript based WYSIWYG html editor, with no dependencies.
http://suneditor.com
MIT License
1.75k stars 313 forks source link

Help with saving editor contents #75

Closed oleteacher closed 5 years ago

oleteacher commented 5 years ago

Hi. We are use to using CKEditor and TinyMCE and so easy to get the contents. Totally confused with SunEditor.

Below is a sample test save code we have been using in the "sample" folder. Could you take a look and make suggestion to get working?

Thanks!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="author" content="https://github.com/JiHong88" />
    <meta name="keywords" content="wysiwyg,editor,javascript,suneditor,wysiwyg eidtor,rich text editor,html editor,contenteditable,위지위그 에디터 웹에디터">
    <meta name="description" content="Pure javascript wysiwyg web editor" />
    <title>Suneditor</title>
    <!-- bootstrap -->
    <link rel="stylesheet" href="bootstrap/bootstrap.css" media="all">
    <script type="text/javascript" src="bootstrap/tether.min.js"></script>
    <script type="text/javascript" src="bootstrap/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="bootstrap/bootstrap.min.js"></script>
    <!-- main css -->
    <link rel="stylesheet" href="css/sample.css" media="all">
    <link rel="stylesheet" href="css/github.css">
    <link rel="stylesheet" href="css/github-frameworks.css">
    <!-- suneditor -->
    <link href="../dist/css/suneditor.min.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="../dist/suneditor.min.js"></script>
    <script>
        $.getJSON('https://api.github.com/repos/JiHong88/SunEditor', function(data) {
            $('#stargazers').html(' Stargazers ' + data.stargazers_count);
        });
    </script>
</head>
<body class="back_index" style="min-width:100%; background-color:#000;">
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html" title="Suneditor"><b>Suneditor</b></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="html/getting-started.html" title="Getting started">Getting started</a></li>
                <li><a href="html/examples.html" title="Examples">Examples</a></li>
                <li><a href="html/customize.html" title="Customize">Customize</a></li>
                <li><a href="html/customPlugins.html" title="CustomPlugins">CustomPlugins</a></li>
                <li><a href="html/document.html" title="Document">Document</a></li>
                <li>&nbsp;&nbsp;</li>
                <li>
                    <a href="https://github.com/JiHong88/SunEditor" target="_blank" id="stargazers" class="github-cat" title="go to github"> Stargazers 00</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="content" style="max-width: 1265px;">
    <form name="frm">
        <textarea id="editor" style="display:none;">
            <p><br></p>
            <div class="sun-editor-id-comp sun-editor-id-image-container float-right" contenteditable="false"><figure class="sun-editor-figure-cover" style="margin: auto;"><img src="http://suneditor.com/sample/img/sunset.jpg" data-align="right" alt="Sunset image" data-rotate="" data-proportion="false" data-rotatex="" data-rotatey="" origin-size="640,360" data-origin="640,360" style="transform: rotate(0deg); max-width: 100%;"><figcaption contenteditable="true" style="margin-top: 0px;"><p>Insert description</p></figcaption></figure></div>
            <h3>The Suneditor is based on pure JavaScript, with no dependencies.</h3>
            <p>Suneditor is a lightweight, flexible, customizable WYSIWYG text editor for your web applications.</p>
            <pre><p>Supports Chrome, Safari, Opeera, Firefox, Edge, IE11, Mobile web browser.</p></pre>
            <p>Suneditor distributed under the <a href="https://github.com/JiHong88/SunEditor/blob/master/LICENSE.txt" target="_blank">MIT</a> license.</p>
            <p><br></p>
        </textarea>
    </form>
    <footer>
        <p>Suneditor create by JiHong.Lee</p>
        <p>Based on javascript</p>
        <p>Suneditor distributed under the MIT license.</p>
        <p><a href="https://github.com/JiHong88/SunEditor" target="_blank" style="color:#f4b124;">go to GitHub suneditor</a></p>
    </footer>
</div>

<script>
    SUNEDITOR.create('editor', {
        display: 'block',
        width: '100%',
        height: 'auto',
        popupDisplay: 'full',
        buttonList: [
            ['undo', 'redo'],
            ['font', 'fontSize', 'formatBlock'],
            ['bold', 'underline', 'italic', 'strike', 'subscript', 'superscript'],
            ['removeFormat'],
            ['fontColor', 'hiliteColor'],
            ['indent', 'outdent'],
            ['align', 'horizontalRule', 'list', 'table'],
            ['link', 'image', 'video'],
            ['fullScreen', 'showBlocks', 'codeView'],
            ['preview', 'print']
        ]
    });
</script>

<!-- START SAVE CODE -->
<br>
<button type="button" value="save" id="save"> Save</button>

<script>
 function saveTextAsFile()
        {
            //var textToWrite = document.getElementById('textArea').value;
            //var textToWrite = CKEDITOR.instances.textArea.getData();
            var textToWrite = editor.getContents();
            var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
            var fileNameToSaveAs = "ecc.html";

            var downloadLink = document.createElement("a");
            downloadLink.download = fileNameToSaveAs;
            downloadLink.innerHTML = "Download File";

            downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
            downloadLink.click();
        }

        var button = document.getElementById('save');
        button.addEventListener('click', saveTextAsFile);
</script>
</body>
</html>
JiHong88 commented 5 years ago

Hi, @oleteacher

Is it an issue that the getContents method is not available?

If so, you must use returned object after creating the editor.

var editor = SUNEDITOR.create('editor', {
        display: 'block',
        width: '100%',
        height: 'auto',
        popupDisplay: 'full',
        buttonList: [
            ['undo', 'redo'],
            ['font', 'fontSize', 'formatBlock'],
            ['bold', 'underline', 'italic', 'strike', 'subscript', 'superscript'],
            ['removeFormat'],
            ['fontColor', 'hiliteColor'],
            ['indent', 'outdent'],
            ['align', 'horizontalRule', 'list', 'table'],
            ['link', 'image', 'video'],
            ['fullScreen', 'showBlocks', 'codeView'],
            ['preview', 'print']
        ]
    });

var textToWrite = editor.getContents();
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = "ecc.html";
...

Thanks : )

oleteacher commented 5 years ago

Thank you @JiHong88 ! That worked for us.

Not good with javascript and must take time to make my skills better:)

Thanks for taking time to help.