funda-ui 4.6.101 → 4.6.111
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/Chatbox/index.d.ts +1 -0
- package/Chatbox/index.js +215 -113
- package/Textarea/index.js +46 -46
- package/Utils/sanitize.d.ts +14 -0
- package/Utils/sanitize.js +87 -0
- package/Utils/useAutosizeTextArea.js +46 -46
- package/lib/cjs/Chatbox/index.d.ts +1 -0
- package/lib/cjs/Chatbox/index.js +215 -113
- package/lib/cjs/Textarea/index.js +46 -46
- package/lib/cjs/Utils/sanitize.d.ts +14 -0
- package/lib/cjs/Utils/sanitize.js +87 -0
- package/lib/cjs/Utils/useAutosizeTextArea.js +46 -46
- package/lib/esm/Chatbox/index.tsx +16 -7
- package/lib/esm/Chatbox/utils/func.ts +0 -54
- package/lib/esm/Textarea/index.tsx +1 -0
- package/lib/esm/Utils/hooks/useAutosizeTextArea.tsx +47 -49
- package/lib/esm/Utils/libs/sanitize.ts +55 -0
- package/package.json +1 -1
|
@@ -713,63 +713,63 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
|
|
|
713
713
|
_ref$maxHeight = _ref.maxHeight,
|
|
714
714
|
maxHeight = _ref$maxHeight === void 0 ? 0 : _ref$maxHeight,
|
|
715
715
|
cb = _ref.cb;
|
|
716
|
-
var _useState = (0, react__WEBPACK_IMPORTED_MODULE_0__.useState)(
|
|
716
|
+
var _useState = (0, react__WEBPACK_IMPORTED_MODULE_0__.useState)(false),
|
|
717
717
|
_useState2 = _slicedToArray(_useState, 2),
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
var _useState3 = (0, react__WEBPACK_IMPORTED_MODULE_0__.useState)(false),
|
|
721
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
722
|
-
defaultRowHeightInit = _useState4[0],
|
|
723
|
-
setDefaultRowHeightInit = _useState4[1];
|
|
718
|
+
defaultRowHeightInit = _useState2[0],
|
|
719
|
+
setDefaultRowHeightInit = _useState2[1];
|
|
724
720
|
|
|
725
721
|
// Reset function to restore default height
|
|
726
722
|
var reset = (0, react__WEBPACK_IMPORTED_MODULE_0__.useCallback)(function () {
|
|
727
|
-
if (el
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
}, [el,
|
|
723
|
+
if (!el) return;
|
|
724
|
+
var scrollHeight = el.scrollHeight;
|
|
725
|
+
el.style.height = scrollHeight + "px";
|
|
726
|
+
|
|
727
|
+
// Get current dimensions after reset
|
|
728
|
+
var style = window.getComputedStyle(el);
|
|
729
|
+
var _controlWidth = el.scrollWidth + parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
|
|
730
|
+
cb === null || cb === void 0 ? void 0 : cb([_controlWidth, scrollHeight]);
|
|
731
|
+
}, [el, cb]);
|
|
736
732
|
(0, react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(function () {
|
|
737
|
-
if (el)
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
733
|
+
if (!el) return;
|
|
734
|
+
|
|
735
|
+
// Initialize default height
|
|
736
|
+
if (!defaultRowHeightInit) {
|
|
737
|
+
el.style.height = 'auto';
|
|
738
|
+
var initialHeight = el.scrollHeight;
|
|
739
|
+
setDefaultRowHeightInit(true);
|
|
740
|
+
|
|
741
|
+
// If the height is 0, set it to "auto"
|
|
742
|
+
if (initialHeight === 0) {
|
|
743
|
+
el.style.height = "auto";
|
|
744
|
+
} else {
|
|
745
|
+
el.style.height = initialHeight + "px";
|
|
749
746
|
}
|
|
747
|
+
}
|
|
750
748
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
}
|
|
749
|
+
// Get dimensions
|
|
750
|
+
var style = window.getComputedStyle(el);
|
|
751
|
+
var _controlWidth = el.scrollWidth + parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
|
|
755
752
|
|
|
756
|
-
|
|
757
|
-
|
|
753
|
+
// Calculate height
|
|
754
|
+
el.style.height = 'auto';
|
|
755
|
+
var finalHeight = el.scrollHeight;
|
|
758
756
|
|
|
759
|
-
|
|
760
|
-
|
|
757
|
+
// Apply max height limit if needed
|
|
758
|
+
if (maxHeight > 0 && finalHeight > maxHeight) {
|
|
759
|
+
finalHeight = maxHeight;
|
|
760
|
+
}
|
|
761
761
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
el.style.height = _scrollHeight + "px";
|
|
769
|
-
}
|
|
770
|
-
cb === null || cb === void 0 ? void 0 : cb([_controlWidth, scrollHeight]);
|
|
762
|
+
// Set final height
|
|
763
|
+
// If the height is 0, set it to "auto"
|
|
764
|
+
if (finalHeight === 0) {
|
|
765
|
+
el.style.height = "auto";
|
|
766
|
+
} else {
|
|
767
|
+
el.style.height = finalHeight + "px";
|
|
771
768
|
}
|
|
772
|
-
|
|
769
|
+
|
|
770
|
+
// Callback
|
|
771
|
+
cb === null || cb === void 0 ? void 0 : cb([_controlWidth, finalHeight]);
|
|
772
|
+
}, [el, value, maxHeight, defaultRowHeightInit]);
|
|
773
773
|
return {
|
|
774
774
|
reset: reset
|
|
775
775
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML entities encode
|
|
3
|
+
*
|
|
4
|
+
* @param {String} str Input text
|
|
5
|
+
* @return {String} Filtered text
|
|
6
|
+
*/ declare function htmlEncode(str: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* HTML entities decode
|
|
9
|
+
*
|
|
10
|
+
* @param {String} str Input text
|
|
11
|
+
* @return {String} Filtered text
|
|
12
|
+
*/
|
|
13
|
+
declare function htmlDecode(str?: string): string;
|
|
14
|
+
export { htmlEncode, htmlDecode };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
2
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
3
|
+
module.exports = factory();
|
|
4
|
+
else if(typeof define === 'function' && define.amd)
|
|
5
|
+
define([], factory);
|
|
6
|
+
else if(typeof exports === 'object')
|
|
7
|
+
exports["RPB"] = factory();
|
|
8
|
+
else
|
|
9
|
+
root["RPB"] = factory();
|
|
10
|
+
})(this, () => {
|
|
11
|
+
return /******/ (() => { // webpackBootstrap
|
|
12
|
+
/******/ "use strict";
|
|
13
|
+
/******/ // The require scope
|
|
14
|
+
/******/ var __webpack_require__ = {};
|
|
15
|
+
/******/
|
|
16
|
+
/************************************************************************/
|
|
17
|
+
/******/ /* webpack/runtime/define property getters */
|
|
18
|
+
/******/ (() => {
|
|
19
|
+
/******/ // define getter functions for harmony exports
|
|
20
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
21
|
+
/******/ for(var key in definition) {
|
|
22
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
23
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
24
|
+
/******/ }
|
|
25
|
+
/******/ }
|
|
26
|
+
/******/ };
|
|
27
|
+
/******/ })();
|
|
28
|
+
/******/
|
|
29
|
+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
30
|
+
/******/ (() => {
|
|
31
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
32
|
+
/******/ })();
|
|
33
|
+
/******/
|
|
34
|
+
/******/ /* webpack/runtime/make namespace object */
|
|
35
|
+
/******/ (() => {
|
|
36
|
+
/******/ // define __esModule on exports
|
|
37
|
+
/******/ __webpack_require__.r = (exports) => {
|
|
38
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
39
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
40
|
+
/******/ }
|
|
41
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
42
|
+
/******/ };
|
|
43
|
+
/******/ })();
|
|
44
|
+
/******/
|
|
45
|
+
/************************************************************************/
|
|
46
|
+
var __webpack_exports__ = {};
|
|
47
|
+
__webpack_require__.r(__webpack_exports__);
|
|
48
|
+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
49
|
+
/* harmony export */ "htmlDecode": () => (/* binding */ htmlDecode),
|
|
50
|
+
/* harmony export */ "htmlEncode": () => (/* binding */ htmlEncode)
|
|
51
|
+
/* harmony export */ });
|
|
52
|
+
/**
|
|
53
|
+
* HTML entities encode
|
|
54
|
+
*
|
|
55
|
+
* @param {String} str Input text
|
|
56
|
+
* @return {String} Filtered text
|
|
57
|
+
*/function htmlEncode(str) {
|
|
58
|
+
return str.replace(/[&<>'"]/g, function (tag) {
|
|
59
|
+
return {
|
|
60
|
+
'&': '&',
|
|
61
|
+
'<': '<',
|
|
62
|
+
'>': '>',
|
|
63
|
+
"'": ''',
|
|
64
|
+
'"': '"'
|
|
65
|
+
}[tag];
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* HTML entities decode
|
|
71
|
+
*
|
|
72
|
+
* @param {String} str Input text
|
|
73
|
+
* @return {String} Filtered text
|
|
74
|
+
*/
|
|
75
|
+
function htmlDecode() {
|
|
76
|
+
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
77
|
+
var entities = [['amp', '&'], ['apos', '\''], ['#x27', '\''], ['#x2F', '/'], ['#39', '\''], ['#47', '/'], ['lt', '<'], ['gt', '>'], ['nbsp', ' '], ['quot', '"'], ['#60', '<'], ['#62', '>']];
|
|
78
|
+
for (var i = 0, max = entities.length; i < max; i++) {
|
|
79
|
+
str = str.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1]);
|
|
80
|
+
}
|
|
81
|
+
return str;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/******/ return __webpack_exports__;
|
|
85
|
+
/******/ })()
|
|
86
|
+
;
|
|
87
|
+
});
|
|
@@ -161,63 +161,63 @@ var useAutosizeTextArea = function useAutosizeTextArea(_ref) {
|
|
|
161
161
|
_ref$maxHeight = _ref.maxHeight,
|
|
162
162
|
maxHeight = _ref$maxHeight === void 0 ? 0 : _ref$maxHeight,
|
|
163
163
|
cb = _ref.cb;
|
|
164
|
-
var _useState = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(
|
|
164
|
+
var _useState = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(false),
|
|
165
165
|
_useState2 = _slicedToArray(_useState, 2),
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
var _useState3 = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(false),
|
|
169
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
170
|
-
defaultRowHeightInit = _useState4[0],
|
|
171
|
-
setDefaultRowHeightInit = _useState4[1];
|
|
166
|
+
defaultRowHeightInit = _useState2[0],
|
|
167
|
+
setDefaultRowHeightInit = _useState2[1];
|
|
172
168
|
|
|
173
169
|
// Reset function to restore default height
|
|
174
170
|
var reset = (0,react__WEBPACK_IMPORTED_MODULE_0__.useCallback)(function () {
|
|
175
|
-
if (el
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}, [el,
|
|
171
|
+
if (!el) return;
|
|
172
|
+
var scrollHeight = el.scrollHeight;
|
|
173
|
+
el.style.height = scrollHeight + "px";
|
|
174
|
+
|
|
175
|
+
// Get current dimensions after reset
|
|
176
|
+
var style = window.getComputedStyle(el);
|
|
177
|
+
var _controlWidth = el.scrollWidth + parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
|
|
178
|
+
cb === null || cb === void 0 ? void 0 : cb([_controlWidth, scrollHeight]);
|
|
179
|
+
}, [el, cb]);
|
|
184
180
|
(0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(function () {
|
|
185
|
-
if (el)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
181
|
+
if (!el) return;
|
|
182
|
+
|
|
183
|
+
// Initialize default height
|
|
184
|
+
if (!defaultRowHeightInit) {
|
|
185
|
+
el.style.height = 'auto';
|
|
186
|
+
var initialHeight = el.scrollHeight;
|
|
187
|
+
setDefaultRowHeightInit(true);
|
|
188
|
+
|
|
189
|
+
// If the height is 0, set it to "auto"
|
|
190
|
+
if (initialHeight === 0) {
|
|
191
|
+
el.style.height = "auto";
|
|
192
|
+
} else {
|
|
193
|
+
el.style.height = initialHeight + "px";
|
|
197
194
|
}
|
|
195
|
+
}
|
|
198
196
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
197
|
+
// Get dimensions
|
|
198
|
+
var style = window.getComputedStyle(el);
|
|
199
|
+
var _controlWidth = el.scrollWidth + parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
|
|
203
200
|
|
|
204
|
-
|
|
205
|
-
|
|
201
|
+
// Calculate height
|
|
202
|
+
el.style.height = 'auto';
|
|
203
|
+
var finalHeight = el.scrollHeight;
|
|
206
204
|
|
|
207
|
-
|
|
208
|
-
|
|
205
|
+
// Apply max height limit if needed
|
|
206
|
+
if (maxHeight > 0 && finalHeight > maxHeight) {
|
|
207
|
+
finalHeight = maxHeight;
|
|
208
|
+
}
|
|
209
209
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
el.style.height = _scrollHeight + "px";
|
|
217
|
-
}
|
|
218
|
-
cb === null || cb === void 0 ? void 0 : cb([_controlWidth, scrollHeight]);
|
|
210
|
+
// Set final height
|
|
211
|
+
// If the height is 0, set it to "auto"
|
|
212
|
+
if (finalHeight === 0) {
|
|
213
|
+
el.style.height = "auto";
|
|
214
|
+
} else {
|
|
215
|
+
el.style.height = finalHeight + "px";
|
|
219
216
|
}
|
|
220
|
-
|
|
217
|
+
|
|
218
|
+
// Callback
|
|
219
|
+
cb === null || cb === void 0 ? void 0 : cb([_controlWidth, finalHeight]);
|
|
220
|
+
}, [el, value, maxHeight, defaultRowHeightInit]);
|
|
221
221
|
return {
|
|
222
222
|
reset: reset
|
|
223
223
|
};
|
|
@@ -9,6 +9,8 @@ import useComId from 'funda-utils/dist/cjs/useComId';
|
|
|
9
9
|
import useDebounce from 'funda-utils/dist/cjs/useDebounce';
|
|
10
10
|
import useThrottle from 'funda-utils/dist/cjs/useThrottle';
|
|
11
11
|
import useClickOutside from 'funda-utils/dist/cjs/useClickOutside';
|
|
12
|
+
import { htmlEncode } from 'funda-utils/dist/cjs/sanitize';
|
|
13
|
+
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
// loader
|
|
@@ -20,8 +22,7 @@ import {
|
|
|
20
22
|
formatLatestDisplayContent,
|
|
21
23
|
formatName,
|
|
22
24
|
fixHtmlTags,
|
|
23
|
-
isStreamResponse
|
|
24
|
-
htmlEncode
|
|
25
|
+
isStreamResponse
|
|
25
26
|
} from './utils/func';
|
|
26
27
|
|
|
27
28
|
import useStreamController from './useStreamController';
|
|
@@ -77,6 +78,7 @@ export type CustomRequestFunction = (
|
|
|
77
78
|
|
|
78
79
|
export type ChatboxProps = {
|
|
79
80
|
debug?: boolean;
|
|
81
|
+
defaultRows?: number;
|
|
80
82
|
prefix?: string;
|
|
81
83
|
contentRef?: React.RefObject<any>;
|
|
82
84
|
model?: string;
|
|
@@ -253,6 +255,7 @@ const Chatbox = (props: ChatboxProps) => {
|
|
|
253
255
|
|
|
254
256
|
const {
|
|
255
257
|
debug,
|
|
258
|
+
defaultRows,
|
|
256
259
|
prefix,
|
|
257
260
|
contentRef,
|
|
258
261
|
model,
|
|
@@ -328,6 +331,7 @@ const Chatbox = (props: ChatboxProps) => {
|
|
|
328
331
|
|
|
329
332
|
return {
|
|
330
333
|
debug,
|
|
334
|
+
defaultRows,
|
|
331
335
|
prefix,
|
|
332
336
|
contentRef,
|
|
333
337
|
model,
|
|
@@ -380,7 +384,7 @@ const Chatbox = (props: ChatboxProps) => {
|
|
|
380
384
|
setActiveButtons(prev => {
|
|
381
385
|
const newState = { ...prev };
|
|
382
386
|
// Turn off only buttons with "isSelect"
|
|
383
|
-
args().toolkitButtons?.forEach((btn, index) => {
|
|
387
|
+
args().toolkitButtons?.forEach((btn: FloatingButton, index: number) => {
|
|
384
388
|
if (btn.isSelect) {
|
|
385
389
|
const _id = `${args().prefix || 'custom-'}chatbox-btn-tools-${chatId}${index}`;
|
|
386
390
|
newState[_id] = false;
|
|
@@ -1300,7 +1304,6 @@ const Chatbox = (props: ChatboxProps) => {
|
|
|
1300
1304
|
{/**------------- CONTROL AREA -------------*/}
|
|
1301
1305
|
<div className="msgcontrol">
|
|
1302
1306
|
|
|
1303
|
-
|
|
1304
1307
|
<Textarea
|
|
1305
1308
|
ref={msInput}
|
|
1306
1309
|
contentRef={inputContentRef}
|
|
@@ -1309,15 +1312,21 @@ const Chatbox = (props: ChatboxProps) => {
|
|
|
1309
1312
|
placeholder={args().placeholder}
|
|
1310
1313
|
disabled={loading ? true : false}
|
|
1311
1314
|
onKeyDown={(event: React.KeyboardEvent) => {
|
|
1312
|
-
|
|
1313
|
-
|
|
1315
|
+
// line breaks
|
|
1316
|
+
if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.ctrlKey && !event.metaKey) {
|
|
1321
|
+
event.preventDefault(); // Prevent line breaks
|
|
1314
1322
|
handleClickSafe();
|
|
1315
1323
|
}
|
|
1324
|
+
|
|
1316
1325
|
}}
|
|
1317
1326
|
onChange={(e) => {
|
|
1318
1327
|
args().onInputChange?.(inputContentRef.current, e.target.value);
|
|
1319
1328
|
}}
|
|
1320
|
-
rows={3}
|
|
1329
|
+
rows={args().defaultRows || 3}
|
|
1321
1330
|
autoSize
|
|
1322
1331
|
autoSizeMaxHeight={200}
|
|
1323
1332
|
/>
|
|
@@ -127,57 +127,3 @@ export function isStreamResponse(response: Response): boolean {
|
|
|
127
127
|
return response.body instanceof ReadableStream;
|
|
128
128
|
};
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* HTML entities encode
|
|
134
|
-
*
|
|
135
|
-
* @param {String} str Input text
|
|
136
|
-
* @return {String} Filtered text
|
|
137
|
-
*/
|
|
138
|
-
export function htmlEncode(str) {
|
|
139
|
-
|
|
140
|
-
return str.replace(/[&<>'"]/g, tag => ({
|
|
141
|
-
'&': '&',
|
|
142
|
-
'<': '<',
|
|
143
|
-
'>': '>',
|
|
144
|
-
"'": ''',
|
|
145
|
-
'"': '"'
|
|
146
|
-
}[tag]));
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* HTML entities decode
|
|
153
|
-
*
|
|
154
|
-
* @param {String} str Input text
|
|
155
|
-
* @return {String} Filtered text
|
|
156
|
-
*/
|
|
157
|
-
export function htmlDecode(str) {
|
|
158
|
-
|
|
159
|
-
let res = '';
|
|
160
|
-
const entities = [
|
|
161
|
-
['amp', '&'],
|
|
162
|
-
['apos', '\''],
|
|
163
|
-
['#x27', '\''],
|
|
164
|
-
['#x2F', '/'],
|
|
165
|
-
['#39', '\''],
|
|
166
|
-
['#47', '/'],
|
|
167
|
-
['lt', '<'],
|
|
168
|
-
['gt', '>'],
|
|
169
|
-
['nbsp', ' '],
|
|
170
|
-
['quot', '"'],
|
|
171
|
-
['#60', '<'],
|
|
172
|
-
['#62', '>']
|
|
173
|
-
];
|
|
174
|
-
|
|
175
|
-
for (let i = 0, max = entities.length; i < max; i++) {
|
|
176
|
-
str = str.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1]);
|
|
177
|
-
}
|
|
178
|
-
res = str;
|
|
179
|
-
|
|
180
|
-
return res;
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
@@ -8,6 +8,7 @@ import { actualPropertyValue, getTextTop } from 'funda-utils/dist/cjs/inputsCalc
|
|
|
8
8
|
import useDebounce from 'funda-utils/dist/cjs/useDebounce';
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
|
|
11
12
|
export type TextareaProps = {
|
|
12
13
|
contentRef?: React.ForwardedRef<any>; // could use "Array" on contentRef.current, such as contentRef.current[0], contentRef.current[1]
|
|
13
14
|
wrapperClassName?: string;
|
|
@@ -51,7 +51,6 @@ const App = () => {
|
|
|
51
51
|
|
|
52
52
|
*/
|
|
53
53
|
import { useEffect, useState, useCallback } from "react";
|
|
54
|
-
|
|
55
54
|
export interface AutosizeTextAreaProps {
|
|
56
55
|
el: any;
|
|
57
56
|
value: string;
|
|
@@ -66,68 +65,67 @@ const useAutosizeTextArea = ({
|
|
|
66
65
|
cb
|
|
67
66
|
}: AutosizeTextAreaProps): { reset: () => void } => {
|
|
68
67
|
|
|
69
|
-
const [defaultRowHeight, setDefaultRowHeight] = useState(0);
|
|
70
68
|
const [defaultRowHeightInit, setDefaultRowHeightInit] = useState(false);
|
|
71
69
|
|
|
72
70
|
// Reset function to restore default height
|
|
73
71
|
const reset = useCallback(() => {
|
|
74
|
-
if (el
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
if (!el) return;
|
|
73
|
+
|
|
74
|
+
const scrollHeight = el.scrollHeight;
|
|
75
|
+
el.style.height = scrollHeight + "px";
|
|
76
|
+
|
|
77
|
+
// Get current dimensions after reset
|
|
78
|
+
const style = window.getComputedStyle(el);
|
|
79
|
+
const _controlWidth = el.scrollWidth + parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
|
|
80
|
+
cb?.([_controlWidth, scrollHeight]);
|
|
81
|
+
}, [el, cb]);
|
|
83
82
|
|
|
84
83
|
useEffect(() => {
|
|
85
|
-
if (el)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
setDefaultRowHeightInit(true);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// restore default row height
|
|
102
|
-
if (defaultRowHeight > 0) {
|
|
103
|
-
el.style.height = defaultRowHeight + "px";
|
|
84
|
+
if (!el) return;
|
|
85
|
+
|
|
86
|
+
// Initialize default height
|
|
87
|
+
if (!defaultRowHeightInit) {
|
|
88
|
+
el.style.height = 'auto';
|
|
89
|
+
const initialHeight = el.scrollHeight;
|
|
90
|
+
setDefaultRowHeightInit(true);
|
|
91
|
+
|
|
92
|
+
// If the height is 0, set it to "auto"
|
|
93
|
+
if (initialHeight === 0) {
|
|
94
|
+
el.style.height = "auto";
|
|
95
|
+
} else {
|
|
96
|
+
el.style.height = initialHeight + "px";
|
|
104
97
|
}
|
|
105
98
|
|
|
106
|
-
|
|
107
|
-
const scrollHeight = el.scrollHeight;
|
|
99
|
+
}
|
|
108
100
|
|
|
101
|
+
// Get dimensions
|
|
102
|
+
const style = window.getComputedStyle(el);
|
|
103
|
+
const _controlWidth = el.scrollWidth + parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
|
|
104
|
+
|
|
105
|
+
// Calculate height
|
|
106
|
+
el.style.height = 'auto';
|
|
107
|
+
let finalHeight = el.scrollHeight;
|
|
108
|
+
|
|
109
|
+
// Apply max height limit if needed
|
|
110
|
+
if (maxHeight > 0 && finalHeight > maxHeight) {
|
|
111
|
+
finalHeight = maxHeight;
|
|
112
|
+
}
|
|
109
113
|
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
// Set final height
|
|
115
|
+
// If the height is 0, set it to "auto"
|
|
116
|
+
if (finalHeight === 0) {
|
|
117
|
+
el.style.height = "auto";
|
|
118
|
+
} else {
|
|
119
|
+
el.style.height = finalHeight + "px";
|
|
120
|
+
}
|
|
112
121
|
|
|
113
|
-
// !!! Compare initial height and changed height
|
|
114
|
-
if (scrollHeight > defaultRowHeight && defaultRowHeight > 0) {
|
|
115
122
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
_scrollHeight = maxHeight;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
el.style.height = _scrollHeight + "px";
|
|
122
|
-
|
|
123
|
-
}
|
|
123
|
+
// Callback
|
|
124
|
+
cb?.([_controlWidth, finalHeight]);
|
|
124
125
|
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
}, [el, value]);
|
|
126
|
+
}, [el, value, maxHeight, defaultRowHeightInit]);
|
|
128
127
|
|
|
129
128
|
return { reset };
|
|
130
129
|
};
|
|
131
130
|
|
|
132
|
-
export default useAutosizeTextArea;
|
|
133
|
-
|
|
131
|
+
export default useAutosizeTextArea;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* HTML entities encode
|
|
4
|
+
*
|
|
5
|
+
* @param {String} str Input text
|
|
6
|
+
* @return {String} Filtered text
|
|
7
|
+
*/function htmlEncode(str: string): string {
|
|
8
|
+
|
|
9
|
+
return str.replace(/[&<>'"]/g, tag => ({
|
|
10
|
+
'&': '&',
|
|
11
|
+
'<': '<',
|
|
12
|
+
'>': '>',
|
|
13
|
+
"'": ''',
|
|
14
|
+
'"': '"'
|
|
15
|
+
}[tag]!));
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* HTML entities decode
|
|
22
|
+
*
|
|
23
|
+
* @param {String} str Input text
|
|
24
|
+
* @return {String} Filtered text
|
|
25
|
+
*/
|
|
26
|
+
function htmlDecode(str: string = ''): string {
|
|
27
|
+
const entities: [string, string][] = [
|
|
28
|
+
['amp', '&'],
|
|
29
|
+
['apos', '\''],
|
|
30
|
+
['#x27', '\''],
|
|
31
|
+
['#x2F', '/'],
|
|
32
|
+
['#39', '\''],
|
|
33
|
+
['#47', '/'],
|
|
34
|
+
['lt', '<'],
|
|
35
|
+
['gt', '>'],
|
|
36
|
+
['nbsp', ' '],
|
|
37
|
+
['quot', '"'],
|
|
38
|
+
['#60', '<'],
|
|
39
|
+
['#62', '>']
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
for (let i = 0, max = entities.length; i < max; i++) {
|
|
43
|
+
str = str.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return str;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
htmlEncode,
|
|
54
|
+
htmlDecode
|
|
55
|
+
}
|