1
votes

So I'm trying to replace 'mybutton's' fuction in the code below with another code that prompts to save the content to a file in a pop-up box. mybutton is what I want to use but change the function from inserting that text "It's my button!" to the function in the code further below.

<script>
tinymce.init({
  selector: 'textarea',
  toolbar: 'mybutton',
  setup: function (editor) {
    editor.addButton('mybutton', {
      text: 'My button',
      icon: false,
      onclick: function () {
        editor.insertContent('&nbsp;<b>It\'s my button!</b>&nbsp;');
      }
    });
  },
  content_css: [
    '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
    '//www.tinymce.com/css/codepen.min.css'
  ]
});
</script>

I'm not sure the proper way to replace the mybutton function's to the function below or what to do with the document.getElement line either. Could I somehow have the mybutton work to this as separate code or does it have to be within tinymce itself? Really no idea, any advice would help.

<script type="text/javascript">
function saveFileAs() {
    if (promptFilename = prompt("Save as ADD EXTENSION", "")) {
        var textBlob = new Blob([document.getElementById("textarea").value], {type:'text/plain'});
        var downloadLink = document.createElement("a");
        downloadLink.download = promptFilename;
        downloadLink.innerHTML = "Download File";
        downloadLink.href = window.URL.createObjectURL(textBlob);
        downloadLink.click();
    }
}

document.getElementById("mybutton").onclick = saveFileAs;
</script>

Hopefully this makes sense, let me know if anything else is needed.

EDIT:

The answer below helped but it doesn't save any NEW data entered into the textarea.

1

1 Answers

0
votes

To answer part of your question you can call any JavaScript that is part of the page from a function in your TinyMCE init. The init is simply a JavaScript object and can interact with other JavaScript on the page. Assuming the saveFileAs() function is loaded on the page you could do this:

tinymce.init({
  selector: 'textarea',
  toolbar: 'mybutton',
  setup: function (editor) {
    editor.addButton('mybutton', {
      text: 'My button',
      icon: false,
      onclick: function (editor) {
        saveFileAs(editor);
      }
    });
  }
});

What are you trying to do in the saveFileAs() method?