This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
AceEditorHOC.jsx
175 lines (154 loc) · 5.1 KB
/
AceEditorHOC.jsx
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
import React from "react";
import ace from "brace";
import AceEditor from "react-ace";
import * as ast from "yaml-ast-parser";
import find from "lodash/find";
const { addListener, removeListener } = ace.acequire("ace/lib/event");
export const PATCH_TOKEN = "TO_BE_MODIFIED";
export class AceEditorHOC extends React.Component {
constructor(props) {
super(props);
this.state = {
activeMarker: [],
markers: [],
};
}
componentDidUpdate(prevProps) {
const { fileToView } = this.props;
if (fileToView !== prevProps.fileToView) {
if (fileToView.baseContent && fileToView.isSupported) {
const markers = this.createMarkers(fileToView);
this.setState({ markers });
}
}
if (
(this.props.overlayOpen !== prevProps.overlayOpen) ||
(this.props.diffOpen !== prevProps.diffOpen)
) {
if (this.aceEditorBase) {
this.aceEditorBase.editor.resize();
}
}
}
componentDidMount() {
addListener(this.aceEditorBase.editor, "click", this.addToOverlay)
addListener(this.aceEditorBase.editor.renderer.scroller, "mousemove", this.setActiveMarker);
addListener(this.aceEditorBase.editor.renderer.scroller, "mouseout", this.setActiveMarker);
}
componentWillUnmount() {
removeListener(this.aceEditorBase.editor, "click", this.addToOverlay);
removeListener(this.aceEditorBase.editor.renderer.scroller, "mousemove", this.setActiveMarker);
removeListener(this.aceEditorBase.editor.renderer.scroller, "mouseout", this.setActiveMarker)
}
findMarkerAtRow = (row, markers) => (
find(markers, ({ startRow, endRow }) => ( row >= startRow && row <= endRow ))
)
addToOverlay = async () => {
const { handleGeneratePatch, handleApplyPatch } = this.props;
const { activeMarker } = this.state;
if (activeMarker.length > 0) {
const matchingMarker = activeMarker[0];
const { path } = matchingMarker;
await handleApplyPatch().catch();
handleGeneratePatch(path);
}
}
setActiveMarker = (e) => {
const { clientY } = e;
const { markers, activeMarker } = this.state;
const renderer = this.aceEditorBase.editor.renderer;
const canvasPos = renderer.scroller.getBoundingClientRect();
const row = Math.ceil((clientY + renderer.scrollTop - canvasPos.top) / renderer.lineHeight);
const matchingMarker = this.findMarkerAtRow(row, markers);
if (matchingMarker) {
renderer.setCursorStyle("pointer");
const [ activeMarker0 = {} ] = activeMarker;
if (matchingMarker.startRow !== activeMarker0.startRow) {
this.setState({ activeMarker: [ matchingMarker ] });
}
} else {
this.setState({ activeMarker: [] });
}
}
createMarkers = (fileToView) => {
if (this.aceEditorBase) {
let markers = [];
const loadedAst = ast.safeLoad(fileToView.baseContent, null);
this.createMarkersRec(loadedAst, [], markers);
return markers;
}
}
createMarkersRec = (ast, path, markers) => {
const aceDoc = this.aceEditorBase.editor.getSession().getDocument();
const createMarkersSlice = ({ items }) => {
if (items && items.length > 0) {
for (let i = 0; i < items.length; i++) {
const item = items[i];
this.createMarkersRec(item, [...path, i], markers);
}
}
};
const createMarkersMap = ({ mappings }) => {
for (const mapping of mappings) {
const { value, key } = mapping;
if (value === null) {
const { startPosition, endPosition } = ast;
const { row: startRow } = aceDoc.indexToPosition(startPosition, 0);
const { row: endRow } = aceDoc.indexToPosition(endPosition, 0);
const nullMarker = {
startRow,
endRow: endRow + 1,
className: "marker-highlight-null",
mapping,
path: [...path, key.value],
}
return markers.push(nullMarker);
}
const newPathKey = key.value;
this.createMarkersRec(value, [...path, newPathKey], markers);
}
};
if (ast.mappings) {
return createMarkersMap(ast);
}
if (ast.items) {
return createMarkersSlice(ast);
}
const { startPosition, endPosition } = ast;
const { row: startRow } = aceDoc.indexToPosition(startPosition, 0);
const { row: endRow } = aceDoc.indexToPosition(endPosition, 0);
const newMarker = {
startRow,
endRow: endRow + 1,
className: "marker-highlight",
mapping: ast,
path,
};
markers.push(newMarker);
}
render() {
const { fileToView } = this.props;
const { activeMarker } = this.state;
return (
<AceEditor
ref={(editor) => { this.aceEditorBase = editor }}
mode="yaml"
theme="chrome"
className="flex1 flex"
readOnly={true}
value={fileToView && fileToView.baseContent || ""}
height="100%"
width="100%"
editorProps={{
$blockScrolling: Infinity,
useSoftTabs: true,
tabSize: 2,
}}
setOptions={{
scrollPastEnd: false
}}
markers={activeMarker}
/>
);
}
}