Skip to content

Commit 0e07cd4

Browse files
committed
Fixed widget not anchored bug
1 parent e9add3a commit 0e07cd4

File tree

1 file changed

+59
-66
lines changed

1 file changed

+59
-66
lines changed

libs/remix-ui/editor/src/lib/remix-ui-editor.tsx

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -362,34 +362,8 @@ export const EditorUI = (props: EditorUIProps) => {
362362
if (restoredWidgets) {
363363
Object.keys(restoredWidgets).forEach((widgetId) => {
364364
const ranges = restoredWidgets[widgetId]
365-
let decoratorList: monacoTypes.editor.IEditorDecorationsCollection
365+
const decoratorList = addDecoratorCollection(widgetId, ranges)
366366

367-
if (ranges.length === 1) {
368-
decoratorList = editorRef.current.createDecorationsCollection([{
369-
range: ranges[0],
370-
options: {
371-
isWholeLine: true,
372-
className: 'newChangesDecoration',
373-
marginClassName: 'newChangesDecoration',
374-
}
375-
}])
376-
} else {
377-
decoratorList = editorRef.current.createDecorationsCollection([{
378-
range: ranges[0],
379-
options: {
380-
isWholeLine: true,
381-
className: 'newChangesDecoration',
382-
marginClassName: 'newChangesDecoration',
383-
}
384-
}, {
385-
range: ranges[1],
386-
options: {
387-
isWholeLine: true,
388-
className: 'modifiedChangesDecoration',
389-
marginClassName: 'modifiedChangesDecoration',
390-
}
391-
}])
392-
}
393367
setTimeout(() => {
394368
const newEntryRange = decoratorList.getRange(0)
395369

@@ -921,30 +895,19 @@ export const EditorUI = (props: EditorUIProps) => {
921895
const docsRange = await props.plugin.call('codeParser', "getLineColumnOfNode", { src: functionNode.documentation.src })
922896
const docs = editorRef.current.getModel().getValueInRange(new monacoRef.current.Range(docsRange.start.line, docsRange.start.column, funcRange.start.line, 1000))
923897
const oldLineCount = (docs || '').split('\n').length - 1
898+
const ranges = [
899+
new monacoRef.current.Range(docsRange.start.line + 1, 0, docsRange.start.line + 1, 0),
900+
new monacoRef.current.Range(docsRange.start.line + newLineCount + 1, 0, docsRange.start.line + newLineCount + oldLineCount, 1000)
901+
]
924902

925903
editorRef.current.executeEdits('docsChange', [
926904
{
927905
range: new monacoRef.current.Range(docsRange.start.line + 1, 0, docsRange.start.line + 1, 0),
928906
text: outputFunctionComments[currentFunction.current] + '\n',
929907
},
930908
])
931-
932-
const decoratorList = editorRef.current.createDecorationsCollection([{
933-
range: new monacoRef.current.Range(docsRange.start.line + 1, 0, docsRange.start.line + newLineCount, 1000),
934-
options: {
935-
isWholeLine: true,
936-
className: 'newChangesDecoration',
937-
marginClassName: 'newChangesDecoration',
938-
}
939-
}, {
940-
range: new monacoRef.current.Range(docsRange.start.line + newLineCount + 1, 0, docsRange.start.line + newLineCount + oldLineCount, 1000),
941-
options: {
942-
isWholeLine: true,
943-
className: 'modifiedChangesDecoration',
944-
marginClassName: 'modifiedChangesDecoration',
945-
}
946-
}])
947909
const widgetId = `accept_decline_widget${Math.random().toString(36).substring(2, 15)}`
910+
const decoratorList = addDecoratorCollection(widgetId, ranges)
948911

949912
setCurrentDiffFile(uri)
950913
setDecoratorListCollection(decoratorListCollection => {
@@ -968,15 +931,9 @@ export const EditorUI = (props: EditorUIProps) => {
968931
text: outputFunctionComments[currentFunction.current] + '\n',
969932
},
970933
])
971-
const decoratorList = editorRef.current.createDecorationsCollection([{
972-
range: new monacoRef.current.Range(funcRange.start.line + 1, 0, funcRange.start.line + newLineCount, 1000),
973-
options: {
974-
isWholeLine: true,
975-
className: 'newChangesDecoration',
976-
marginClassName: 'newChangesDecoration',
977-
}
978-
}])
934+
const ranges = [new monacoRef.current.Range(funcRange.start.line + 1, 0, funcRange.start.line + newLineCount, 1000)]
979935
const widgetId = `accept_decline_widget${Math.random().toString(36).substring(2, 15)}`
936+
const decoratorList = addDecoratorCollection(widgetId, ranges)
980937

981938
setCurrentDiffFile(uri)
982939
setDecoratorListCollection(decoratorListCollection => {
@@ -1382,6 +1339,52 @@ export const EditorUI = (props: EditorUIProps) => {
13821339
})
13831340
}
13841341

1342+
function addDecoratorCollection (widgetId: string, ranges: monacoTypes.IRange[]): monacoTypes.editor.IEditorDecorationsCollection {
1343+
let decoratorList: monacoTypes.editor.IEditorDecorationsCollection
1344+
if (ranges.length === 1) {
1345+
decoratorList = editorRef.current.createDecorationsCollection([{
1346+
range: ranges[0],
1347+
options: {
1348+
isWholeLine: true,
1349+
className: 'newChangesDecoration',
1350+
marginClassName: 'newChangesDecoration',
1351+
}
1352+
}])
1353+
} else {
1354+
decoratorList = editorRef.current.createDecorationsCollection([{
1355+
range: ranges[0],
1356+
options: {
1357+
isWholeLine: true,
1358+
className: 'newChangesDecoration',
1359+
marginClassName: 'newChangesDecoration',
1360+
}
1361+
}, {
1362+
range: ranges[1],
1363+
options: {
1364+
isWholeLine: true,
1365+
className: 'modifiedChangesDecoration',
1366+
marginClassName: 'modifiedChangesDecoration',
1367+
}
1368+
}])
1369+
}
1370+
1371+
decoratorList.onDidChange(() => {
1372+
const newRanges = decoratorList.getRanges()
1373+
1374+
if (newRanges.length === 0) return
1375+
if (newRanges[0].startLineNumber !== ranges[0].startLineNumber && document.getElementById(widgetId)) {
1376+
editorRef.current.removeContentWidget({
1377+
getId: () => widgetId
1378+
})
1379+
1380+
addAcceptDeclineWidget(widgetId, editorRef.current, { column: 0, lineNumber: newRanges[0].startLineNumber + 1 }, () => acceptHandler(decoratorList, widgetId), () => rejectHandler(decoratorList, widgetId))
1381+
}
1382+
ranges = newRanges
1383+
})
1384+
1385+
return decoratorList
1386+
}
1387+
13851388
function showCustomDiff (uri: string) {
13861389
const lineChanges: monacoTypes.editor.ILineChange[] = diffEditorRef.current.getLineChanges()
13871390
let totalLineDifference = 0
@@ -1402,22 +1405,12 @@ export const EditorUI = (props: EditorUIProps) => {
14021405
},
14031406
])
14041407

1405-
const decoratorList = editorRef.current.createDecorationsCollection([{
1406-
range: new monacoRef.current.Range(modifiedStartLine, 0, modifiedEndLine, 1000),
1407-
options: {
1408-
isWholeLine: true,
1409-
className: 'newChangesDecoration',
1410-
marginClassName: 'newChangesDecoration',
1411-
}
1412-
}, {
1413-
range: new monacoRef.current.Range(originalStartLine, 0, originalEndLine, 1000),
1414-
options: {
1415-
isWholeLine: true,
1416-
className: 'modifiedChangesDecoration',
1417-
marginClassName: 'modifiedChangesDecoration',
1418-
}
1419-
}])
1408+
const ranges = [
1409+
new monacoRef.current.Range(modifiedStartLine, 0, modifiedEndLine, 1000),
1410+
new monacoRef.current.Range(originalStartLine, 0, originalEndLine, 1000)
1411+
]
14201412
const widgetId = `accept_decline_widget${index}`
1413+
const decoratorList = addDecoratorCollection(widgetId, ranges)
14211414

14221415
setDecoratorListCollection(decoratorListCollection => ({ ...decoratorListCollection, [widgetId]: decoratorList }))
14231416
if (index === 0) {

0 commit comments

Comments
 (0)