@pie-lib/text-select 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/legend.d.ts +13 -0
- package/dist/legend.d.ts.map +1 -0
- package/dist/legend.js +64 -0
- package/dist/text-select.d.ts +35 -0
- package/dist/text-select.d.ts.map +1 -0
- package/dist/text-select.js +53 -0
- package/dist/token-select/index.d.ts +39 -0
- package/dist/token-select/index.d.ts.map +1 -0
- package/dist/token-select/index.js +102 -0
- package/dist/token-select/token.d.ts +33 -0
- package/dist/token-select/token.d.ts.map +1 -0
- package/dist/token-select/token.js +134 -0
- package/dist/tokenizer/builder.d.ts +28 -0
- package/dist/tokenizer/builder.d.ts.map +1 -0
- package/dist/tokenizer/builder.js +124 -0
- package/dist/tokenizer/controls.d.ts +24 -0
- package/dist/tokenizer/controls.d.ts.map +1 -0
- package/dist/tokenizer/controls.js +68 -0
- package/dist/tokenizer/index.d.ts +36 -0
- package/dist/tokenizer/index.d.ts.map +1 -0
- package/dist/tokenizer/index.js +91 -0
- package/dist/tokenizer/selection-utils.d.ts +11 -0
- package/dist/tokenizer/selection-utils.d.ts.map +1 -0
- package/dist/tokenizer/selection-utils.js +18 -0
- package/dist/tokenizer/token-text.d.ts +28 -0
- package/dist/tokenizer/token-text.d.ts.map +1 -0
- package/dist/tokenizer/token-text.js +85 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +21 -0
- package/package.json +39 -0
- package/src/index.ts +18 -0
- package/src/legend.tsx +112 -0
- package/src/text-select.tsx +89 -0
- package/src/token-select/index.tsx +181 -0
- package/src/token-select/token.tsx +233 -0
- package/src/tokenizer/builder.ts +268 -0
- package/src/tokenizer/controls.tsx +81 -0
- package/src/tokenizer/index.tsx +154 -0
- package/src/tokenizer/selection-utils.ts +59 -0
- package/src/tokenizer/token-text.tsx +145 -0
- package/src/utils.tsx +66 -0
package/src/utils.tsx
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* @synced-from pie-lib/packages/text-select/src/utils.js
|
|
4
|
+
* @auto-generated
|
|
5
|
+
*
|
|
6
|
+
* This file is automatically synced from pie-elements and converted to TypeScript.
|
|
7
|
+
* Manual edits will be overwritten on next sync.
|
|
8
|
+
* To make changes, edit the upstream JavaScript file and run sync again.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const createElementFromHTML = (htmlString = '') => {
|
|
12
|
+
const div = document.createElement('div');
|
|
13
|
+
|
|
14
|
+
div.innerHTML = htmlString.trim();
|
|
15
|
+
|
|
16
|
+
return div;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const parseBrs = (dom) => {
|
|
20
|
+
const brs = dom.querySelectorAll('br');
|
|
21
|
+
|
|
22
|
+
brs.forEach((br) => br.replaceWith('\n'));
|
|
23
|
+
|
|
24
|
+
dom.innerHTML = dom.innerHTML.replace(/\n\n/g, '\n');
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const parseParagraph = (paragraph, end) => {
|
|
28
|
+
if (end) {
|
|
29
|
+
return paragraph.innerHTML;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return `${paragraph.innerHTML}\n\n`;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const parseParagraphs = (dom) => {
|
|
36
|
+
const paragraphs = dom.querySelectorAll('p');
|
|
37
|
+
// separate variable for easily debugging, if needed
|
|
38
|
+
let str = '';
|
|
39
|
+
|
|
40
|
+
paragraphs.forEach((par, index) => {
|
|
41
|
+
str += parseParagraph(par, index === paragraphs.length - 1);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return str || null;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const prepareText = (text) => {
|
|
48
|
+
let txtDom = createElementFromHTML(text);
|
|
49
|
+
const allDomElements = Array.from(txtDom.querySelectorAll('*'));
|
|
50
|
+
|
|
51
|
+
if (txtDom.querySelectorAll('p').length === 0) {
|
|
52
|
+
const div = document.createElement('div');
|
|
53
|
+
|
|
54
|
+
div.innerHTML = `<p>${txtDom.innerHTML}</p>`;
|
|
55
|
+
txtDom = div;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// if no dom elements, we just return the text
|
|
59
|
+
if (allDomElements.length === 0) {
|
|
60
|
+
return text;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
parseBrs(txtDom);
|
|
64
|
+
|
|
65
|
+
return parseParagraphs(txtDom);
|
|
66
|
+
};
|