-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathutil.ts
177 lines (170 loc) · 6.32 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Defines common util methods used by Rich Text Editor.
*/
import { isNullOrUndefined, Browser, createElement, detach } from '@syncfusion/ej2-base';
import { IToolbarStatus } from './interface';
import * as classes from '../rich-text-editor/base/classes';
const inlineNode: string[] = ['a', 'abbr', 'acronym', 'audio', 'b', 'bdi', 'bdo', 'big', 'br', 'button',
'canvas', 'cite', 'code', 'data', 'datalist', 'del', 'dfn', 'em', 'embed', 'font', 'i', 'iframe', 'img', 'input',
'ins', 'kbd', 'label', 'map', 'mark', 'meter', 'noscript', 'object', 'output', 'picture', 'progress',
'q', 'ruby', 's', 'samp', 'script', 'select', 'slot', 'small', 'span', 'strong', 'strike', 'sub', 'sup', 'svg',
'template', 'textarea', 'time', 'u', 'tt', 'var', 'video', 'wbr'];
/**
* @returns {void}
* @hidden
*/
export function isIDevice(): boolean {
let result: boolean = false;
if (Browser.isDevice && Browser.isIos) {
result = true;
}
return result;
}
/**
* @param {Element} editableElement - specifies the editable element.
* @param {string} selector - specifies the string values.
* @returns {void}
* @hidden
*/
export function setEditFrameFocus(editableElement: Element, selector: string): void {
if (editableElement.nodeName === 'BODY' && !isNullOrUndefined(selector)) {
const iframe: HTMLIFrameElement = <HTMLIFrameElement>top.window.document.querySelector(selector);
if (!isNullOrUndefined(iframe)) {
iframe.contentWindow.focus();
}
}
}
/**
* @param {string} value - specifies the string value
* @returns {void}
* @hidden
*/
export function updateTextNode(value: string): string {
const tempNode: HTMLElement = document.createElement('div');
tempNode.innerHTML = value;
tempNode.setAttribute('class', 'tempDiv');
const resultElm: HTMLElement = document.createElement('div');
const childNodes: NodeListOf<Node> = tempNode.childNodes as NodeListOf<Node>;
if (childNodes.length > 0) {
let isPreviousInlineElem: boolean;
let previousParent: HTMLElement;
let paraElm: HTMLElement;
while (tempNode.firstChild) {
if ((tempNode.firstChild.nodeName === '#text' &&
(tempNode.firstChild.textContent.indexOf('\n') < 0 || tempNode.firstChild.textContent.trim() !== '')) ||
inlineNode.indexOf(tempNode.firstChild.nodeName.toLocaleLowerCase()) >= 0 ) {
if (!isPreviousInlineElem) {
paraElm = createElement('p');
resultElm.appendChild(paraElm);
paraElm.appendChild(tempNode.firstChild);
} else {
previousParent.appendChild(tempNode.firstChild);
}
previousParent = paraElm;
isPreviousInlineElem = true;
} else if (tempNode.firstChild.nodeName === '#text' && (tempNode.firstChild.textContent === '\n' ||
(tempNode.firstChild.textContent.indexOf('\n') >= 0 && tempNode.firstChild.textContent.trim() === ''))) {
detach(tempNode.firstChild);
} else {
resultElm.appendChild(tempNode.firstChild);
isPreviousInlineElem = false;
}
}
const tableElm: NodeListOf<HTMLElement> = resultElm.querySelectorAll('table');
for (let i: number = 0; i < tableElm.length; i++) {
if (tableElm[i].getAttribute('border') === '0') {
tableElm[i].removeAttribute('border');
}
const tdElm: NodeListOf<HTMLElement> = tableElm[i].querySelectorAll('td');
for (let j: number = 0; j < tdElm.length; j++) {
if (tdElm[j].style.borderLeft === 'none') {
tdElm[j].style.removeProperty('border-left');
}
if (tdElm[j].style.borderRight === 'none') {
tdElm[j].style.removeProperty('border-right');
}
if (tdElm[j].style.borderBottom === 'none') {
tdElm[j].style.removeProperty('border-bottom');
}
if (tdElm[j].style.borderTop === 'none') {
tdElm[j].style.removeProperty('border-top');
}
if (tdElm[j].style.border === 'none') {
tdElm[j].style.removeProperty('border');
}
}
if (!tableElm[i].classList.contains(classes.CLS_TABLE)) {
tableElm[i].classList.add(classes.CLS_TABLE);
}
}
const imageElm: NodeListOf<HTMLElement> = resultElm.querySelectorAll('img');
for (let i: number = 0; i < imageElm.length; i++) {
if (!imageElm[i].classList.contains(classes.CLS_RTE_IMAGE)) {
imageElm[i].classList.add(classes.CLS_RTE_IMAGE);
}
if (!(imageElm[i].classList.contains(classes.CLS_IMGINLINE) ||
imageElm[i].classList.contains(classes.CLS_IMGBREAK))) {
imageElm[i].classList.add(classes.CLS_IMGINLINE);
}
}
}
return resultElm.innerHTML;
}
/**
* @param {Node} startChildNodes - specifies the node
* @returns {void}
* @hidden
*/
export function getLastTextNode(startChildNodes: Node): Node {
let finalNode: Node = startChildNodes;
do {
if (finalNode.childNodes.length > 0) {
finalNode = finalNode.childNodes[0];
}
}
while (finalNode.childNodes.length > 0);
return finalNode;
}
/**
* @returns {void}
* @hidden
*/
export function getDefaultHtmlTbStatus(): IToolbarStatus {
return {
bold: false,
italic: false,
subscript: false,
superscript: false,
strikethrough: false,
orderedlist: false,
unorderedlist: false,
underline: false,
alignments: null,
backgroundcolor: null,
fontcolor: null,
fontname: null,
fontsize: null,
formats: null,
createlink: false,
insertcode: false
};
}
/**
* @returns {void}
* @hidden
*/
export function getDefaultMDTbStatus(): IToolbarStatus {
return {
bold: false,
italic: false,
subscript: false,
superscript: false,
strikethrough: false,
orderedlist: false,
uppercase: false,
lowercase: false,
inlinecode: false,
unorderedlist: false,
formats: null
};
}