@nethru/ui 1.0.55 → 1.0.57
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/Editor.js +62 -5
- package/dist/Frame.js +0 -18
- package/package.json +1 -1
package/dist/Editor.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { Box, Typography } from "@mui/material";
|
|
1
3
|
import CodeMirror from '@uiw/react-codemirror';
|
|
2
4
|
import { javascript } from '@codemirror/lang-javascript';
|
|
3
5
|
import { json } from '@codemirror/lang-json';
|
|
4
6
|
import { EditorView } from "@codemirror/view";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
+
import { RegExpCursor, SearchCursor } from '@codemirror/search';
|
|
8
|
+
import { StateEffect, StateField } from "@codemirror/state";
|
|
9
|
+
import { Decoration } from "@codemirror/view";
|
|
7
10
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
8
11
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
9
12
|
export default function Editor({
|
|
@@ -14,11 +17,14 @@ export default function Editor({
|
|
|
14
17
|
basicSetup,
|
|
15
18
|
extensions = [],
|
|
16
19
|
jsonFormat = false,
|
|
20
|
+
keyword,
|
|
21
|
+
isRegExp = false,
|
|
17
22
|
onUpdate,
|
|
18
23
|
style,
|
|
19
24
|
...props
|
|
20
25
|
}) {
|
|
21
26
|
const ref = useRef();
|
|
27
|
+
const [view, setView] = useState();
|
|
22
28
|
const [content, setContent] = useState('');
|
|
23
29
|
const [focused, setFocused] = useState(false);
|
|
24
30
|
const styles = useMemo(() => {
|
|
@@ -28,13 +34,63 @@ export default function Editor({
|
|
|
28
34
|
};
|
|
29
35
|
// eslint-disable-next-line
|
|
30
36
|
}, [readOnly, error, focused, ref.current]);
|
|
37
|
+
const highlight = useMemo(() => {
|
|
38
|
+
const highlightEffect = StateEffect.define();
|
|
39
|
+
const decoration = Decoration.mark({
|
|
40
|
+
attributes: {
|
|
41
|
+
style: "background-color:#e9db5d" //#90caf9
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const extension = StateField.define({
|
|
46
|
+
create() {
|
|
47
|
+
return Decoration.none;
|
|
48
|
+
},
|
|
49
|
+
update(value, transaction) {
|
|
50
|
+
value = value.map(transaction.changes);
|
|
51
|
+
for (let effect of transaction.effects) {
|
|
52
|
+
if (effect.is(highlightEffect)) {
|
|
53
|
+
value = value.update({
|
|
54
|
+
add: effect.value,
|
|
55
|
+
sort: true
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
},
|
|
61
|
+
provide: field => EditorView.decorations.from(field)
|
|
62
|
+
});
|
|
63
|
+
return {
|
|
64
|
+
effect: highlightEffect,
|
|
65
|
+
decoration: decoration,
|
|
66
|
+
extension: extension
|
|
67
|
+
};
|
|
68
|
+
}, []);
|
|
31
69
|
const handleUpdate = viewUpdate => {
|
|
32
70
|
setFocused(viewUpdate.view.hasFocus);
|
|
33
71
|
if (onUpdate) onUpdate(viewUpdate);
|
|
34
72
|
};
|
|
73
|
+
const highlightKeyword = useCallback(() => {
|
|
74
|
+
if (!view || !keyword) return;
|
|
75
|
+
const {
|
|
76
|
+
effect,
|
|
77
|
+
decoration
|
|
78
|
+
} = highlight;
|
|
79
|
+
const cursor = isRegExp ? new RegExpCursor(view.state.doc, keyword) : new SearchCursor(view.state.doc, keyword, undefined, undefined, s => s.toLowerCase());
|
|
80
|
+
while (true) {
|
|
81
|
+
cursor.next();
|
|
82
|
+
if (cursor.done) break;
|
|
83
|
+
view.dispatch({
|
|
84
|
+
effects: effect.of([decoration.range(cursor.value.from, cursor.value.to)])
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}, [view, highlight, keyword, isRegExp]);
|
|
35
88
|
useEffect(() => {
|
|
36
|
-
setTimeout(() =>
|
|
37
|
-
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
setContent(value);
|
|
91
|
+
highlightKeyword();
|
|
92
|
+
}, 100);
|
|
93
|
+
}, [value, view, highlightKeyword]);
|
|
38
94
|
return /*#__PURE__*/_jsxs(Box, {
|
|
39
95
|
ref: ref,
|
|
40
96
|
children: [/*#__PURE__*/_jsx(CodeMirror, {
|
|
@@ -42,12 +98,13 @@ export default function Editor({
|
|
|
42
98
|
editable: !readOnly,
|
|
43
99
|
extensions: [jsonFormat ? json() : javascript({
|
|
44
100
|
jsx: true
|
|
45
|
-
}), EditorView.lineWrapping, ...extensions],
|
|
101
|
+
}), EditorView.lineWrapping, highlight.extension, ...extensions],
|
|
46
102
|
basicSetup: {
|
|
47
103
|
...basicSetup,
|
|
48
104
|
highlightActiveLineGutter: false,
|
|
49
105
|
highlightActiveLine: !readOnly
|
|
50
106
|
},
|
|
107
|
+
onCreateEditor: (view, state) => setView(view),
|
|
51
108
|
onUpdate: handleUpdate,
|
|
52
109
|
style: {
|
|
53
110
|
...style,
|
package/dist/Frame.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from "react";
|
|
2
1
|
import { Box } from '@mui/material';
|
|
3
2
|
import { SidebarContextProvider } from './sidebar/SidebarContext';
|
|
4
3
|
import { variables } from './variables';
|
|
@@ -13,8 +12,6 @@ export default function Frame({
|
|
|
13
12
|
menu,
|
|
14
13
|
menuIcons = {}
|
|
15
14
|
}) {
|
|
16
|
-
const ref = useRef();
|
|
17
|
-
const [width, setWidth] = useState('unset');
|
|
18
15
|
const containerStyles = {
|
|
19
16
|
display: 'flex',
|
|
20
17
|
flexGrow: 1,
|
|
@@ -27,17 +24,6 @@ export default function Frame({
|
|
|
27
24
|
overflow: 'auto',
|
|
28
25
|
padding: '20px 40px'
|
|
29
26
|
};
|
|
30
|
-
useEffect(() => {
|
|
31
|
-
if (!ref.current) return;
|
|
32
|
-
const observer = new ResizeObserver(entries => {
|
|
33
|
-
const width = entries.length > 0 ? entries[0].borderBoxSize[0].inlineSize : 0;
|
|
34
|
-
setWidth(width > 0 ? `${width}px` : 'unset');
|
|
35
|
-
});
|
|
36
|
-
observer.observe(ref.current);
|
|
37
|
-
return () => {
|
|
38
|
-
observer.disconnect();
|
|
39
|
-
};
|
|
40
|
-
}, []);
|
|
41
27
|
return /*#__PURE__*/_jsxs(_Fragment, {
|
|
42
28
|
children: [appBar, /*#__PURE__*/_jsxs(Box, {
|
|
43
29
|
sx: containerStyles,
|
|
@@ -46,13 +32,9 @@ export default function Frame({
|
|
|
46
32
|
icons: menuIcons,
|
|
47
33
|
children: sideBar
|
|
48
34
|
}), /*#__PURE__*/_jsxs(Box, {
|
|
49
|
-
ref: ref,
|
|
50
35
|
component: "section",
|
|
51
36
|
sx: contentStyles,
|
|
52
37
|
children: [/*#__PURE__*/_jsx("main", {
|
|
53
|
-
style: {
|
|
54
|
-
maxWidth: width
|
|
55
|
-
},
|
|
56
38
|
children: children
|
|
57
39
|
}), footer]
|
|
58
40
|
})]
|