comparison src/development/plugins/alternative-lists.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 Inline-Code Plugin for BBCode format
3 * http://www.sceditor.com/
4 *
5 * Copyright (C) 2011-2013, Sam Clarke (samclarke.com)
6 *
7 * SCEditor is licensed under the MIT license:
8 * http://www.opensource.org/licenses/mit-license.php
9 *
10 * @fileoverview SCEditor alternative lists plugin
11 * This plugin implements phpBB style of the lists:
12 * [list]
13 * [*]item
14 * [*]item
15 * [/list]
16 * @author Alex Betis
17 */
18
19 (function (sceditor) {
20 'use strict';
21
22 var utils = sceditor.utils;
23
24 function isFunction(fn) {
25 return typeof fn === 'function';
26 }
27
28 sceditor.plugins['alternative-lists'] = function () {
29 var base = this;
30
31 /**
32 * Private functions
33 * @private
34 */
35 var bulletHandler;
36 var orderedHandler;
37 var insertListTag;
38
39 base.init = function () {
40 var opts = this.opts;
41
42 // Enable for BBCode only
43 if (opts.format && opts.format !== 'bbcode') {
44 return;
45 }
46
47 // Override only txtExec implementation
48 sceditor.command.get('orderedlist').txtExec = orderedHandler;
49 sceditor.command.get('bulletlist').txtExec = bulletHandler;
50
51 // Override current implementation
52 sceditor.formats.bbcode.set('list', {
53 breakStart: true,
54 isInline: false,
55 skipLastLineBreak: true,
56 html: function (token, attrs, content) {
57 var listType = 'disc';
58 var toHtml = null;
59
60 if (attrs.defaultattr) {
61 listType = attrs.defaultattr;
62 }
63
64 if (listType === '1') {
65 // This listType belongs to orderedList (OL)
66 toHtml = sceditor.formats.bbcode.get('ol').html;
67 } else {
68 // unknown listType, use default bullet list behavior
69 toHtml = sceditor.formats.bbcode.get('ul').html;
70 }
71
72 if (isFunction(toHtml)) {
73 return toHtml.call(this, token, attrs, content);
74 } else {
75 token.attrs['0'] = content;
76 return sceditor.formats.bbcode.formatBBCodeString(
77 toHtml, token.attrs);
78 }
79 }
80 });
81
82 sceditor.formats.bbcode.set('ul', {
83 tags: {
84 ul: null
85 },
86 breakStart: true,
87 isInline: false,
88 skipLastLineBreak: true,
89 format: '[list]{0}[/list]',
90 html: '<ul>{0}</ul>'
91 });
92
93 sceditor.formats.bbcode.set('ol', {
94 tags: {
95 ol: null
96 },
97 breakStart: true,
98 isInline: false,
99 skipLastLineBreak: true,
100 format: '[list=1]{0}[/list]',
101 html: '<ol>{0}</ol>'
102 });
103
104 sceditor.formats.bbcode.set('li', {
105 tags: {
106 li: null
107 },
108 isInline: false,
109 closedBy: ['/ul', '/ol', '/list', '*', 'li'],
110 format: '[*]{0}',
111 html: '<li>{0}</li>'
112 });
113
114 sceditor.formats.bbcode.set('*', {
115 isInline: false,
116 excludeClosing: true,
117 closedBy: ['/ul', '/ol', '/list', '*', 'li'],
118 html: '<li>{0}</li>'
119 });
120 };
121
122 insertListTag = function (editor, listType, selected) {
123 var content = '';
124
125 utils.each(selected.split(/\r?\n/), function (item) {
126 content += (content ? '\n' : '') +
127 '[*]' + item;
128 });
129
130 if (listType === '') {
131 editor.insertText('[list]\n' + content + '\n[/list]');
132 } else {
133 editor.insertText('[list=' + listType + ']\n' + content +
134 '\n[/list]');
135 }
136 };
137
138 /**
139 * Function for the txtExec and exec properties
140 *
141 * @param {node} caller
142 * @private
143 */
144 orderedHandler = function (caller, selected) {
145 var editor = this;
146
147 insertListTag(editor, '1', selected);
148 };
149
150 bulletHandler = function (caller, selected) {
151 var editor = this;
152
153 insertListTag(editor, '', selected);
154 };
155
156 };
157 })(sceditor);