comparison src/development/plugins/v1compat.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 * Version 1 compatibility plugin
3 *
4 * Patches commands and BBCodes set with
5 * command.set and bbcode.set to wrap DOM
6 * node arguments in jQuery objects.
7 *
8 * Should only be used to ease migrating.
9 */
10 (function (sceditor, $) {
11 'use strict';
12
13 var plugins = sceditor.plugins;
14
15 /**
16 * Patches a method to wrap and DOM nodes in a jQuery object
17 * @private
18 */
19 function patchMethodArguments(fn) {
20 if (fn._scePatched) {
21 return fn;
22 }
23
24 var patch = function () {
25 var args = [];
26
27 for (var i = 0; i < arguments.length; i++) {
28 var arg = arguments[i];
29
30 if (arg && arg.nodeType) {
31 args.push($(arg));
32 } else {
33 args.push(arg);
34 }
35 }
36
37 return fn.apply(this, args);
38 };
39
40 patch._scePatched = true;
41 return patch;
42 }
43
44 /**
45 * Patches a method to wrap any return value in a jQuery object
46 * @private
47 */
48 function patchMethodReturn(fn) {
49 if (fn._scePatched) {
50 return fn;
51 }
52
53 var patch = function () {
54 return $(fn.apply(this, arguments));
55 };
56
57 patch._scePatched = true;
58 return patch;
59 }
60
61 var oldSet = sceditor.command.set;
62 sceditor.command.set = function (name, cmd) {
63 if (cmd && typeof cmd.exec === 'function') {
64 cmd.exec = patchMethodArguments(cmd.exec);
65 }
66
67 if (cmd && typeof cmd.txtExec === 'function') {
68 cmd.txtExec = patchMethodArguments(cmd.txtExec);
69 }
70
71 return oldSet.call(this, name, cmd);
72 };
73
74 if (plugins.bbcode) {
75 var oldBBCodeSet = plugins.bbcode.bbcode.set;
76 plugins.bbcode.bbcode.set = function (name, bbcode) {
77 if (bbcode && typeof bbcode.format === 'function') {
78 bbcode.format = patchMethodArguments(bbcode.format);
79 }
80
81 return oldBBCodeSet.call(this, name, bbcode);
82 };
83 };
84
85 var oldCreate = sceditor.create;
86 sceditor.create = function (textarea, options) {
87 oldCreate.call(this, textarea, options);
88
89 if (textarea && textarea._sceditor) {
90 var editor = textarea._sceditor;
91
92 editor.getBody = patchMethodReturn(editor.getBody);
93 editor.getContentAreaContainer =
94 patchMethodReturn(editor.getContentAreaContainer);
95 }
96 };
97 }(sceditor, jQuery));