comparison src/development/plugins/plaintext.js @ 0:4c4fc447baea

start with sceditor-3.1.1
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 04 Aug 2022 15:21:29 -0600
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c4fc447baea
1 /**
2 * SCEditor Plain Text Plugin
3 * http://www.sceditor.com/
4 *
5 * Copyright (C) 2016, Sam Clarke (samclarke.com)
6 *
7 * SCEditor is licensed under the MIT license:
8 * http://www.opensource.org/licenses/mit-license.php
9 *
10 * @author Sam Clarke
11 */
12 (function (sceditor) {
13 'use strict';
14
15 var utils = sceditor.utils;
16 var dom = sceditor.dom;
17
18 /**
19 * Options:
20 *
21 * pastetext.addButton - If to replace the plaintext button with a toggle
22 * button that enables and disables plain text mode.
23 *
24 * pastetext.enabled - If the plain text button should be enabled at start
25 * up. Only applies if addButton is enabled.
26 */
27 sceditor.plugins.plaintext = function () {
28 var plainTextEnabled = true;
29
30 this.init = function () {
31 var commands = this.commands;
32 var opts = this.opts;
33
34 if (opts && opts.plaintext && opts.plaintext.addButton) {
35 plainTextEnabled = opts.plaintext.enabled;
36
37 commands.pastetext = utils.extend(commands.pastetext || {}, {
38 state: function () {
39 return plainTextEnabled ? 1 : 0;
40 },
41 exec: function () {
42 plainTextEnabled = !plainTextEnabled;
43 }
44 });
45 }
46 };
47
48 this.signalPasteRaw = function (data) {
49 if (plainTextEnabled) {
50 if (data.html && !data.text) {
51 var div = document.createElement('div');
52 div.innerHTML = data.html;
53
54 // TODO: Refactor into private shared module with editor
55 // innerText adds two newlines after <p> tags so convert
56 // them to <div> tags
57 utils.each(div.querySelectorAll('p'), function (_, elm) {
58 dom.convertElement(elm, 'div');
59 });
60 // Remove collapsed <br> tags as innerText converts them to
61 // newlines
62 utils.each(div.querySelectorAll('br'), function (_, elm) {
63 if (!elm.nextSibling ||
64 !dom.isInline(elm.nextSibling, true)) {
65 elm.parentNode.removeChild(elm);
66 }
67 });
68
69 document.body.appendChild(div);
70 data.text = div.innerText;
71 document.body.removeChild(div);
72 }
73
74 data.html = null;
75 }
76 };
77 };
78 }(sceditor));