mirror of
https://github.com/open-webui/open-webui
synced 2025-04-24 08:16:02 +00:00
refac: code update behaviour
This commit is contained in:
parent
cdb5eae43a
commit
41a83f1dc5
@ -33,15 +33,49 @@
|
|||||||
|
|
||||||
const updateValue = () => {
|
const updateValue = () => {
|
||||||
if (_value !== value) {
|
if (_value !== value) {
|
||||||
|
const changes = findChanges(_value, value);
|
||||||
_value = value;
|
_value = value;
|
||||||
if (codeEditor) {
|
|
||||||
codeEditor.dispatch({
|
if (codeEditor && changes.length > 0) {
|
||||||
changes: [{ from: 0, to: codeEditor.state.doc.length, insert: _value }]
|
codeEditor.dispatch({ changes });
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds multiple diffs in two strings and generates minimal change edits.
|
||||||
|
*/
|
||||||
|
function findChanges(oldStr, newStr) {
|
||||||
|
let changes = [];
|
||||||
|
let oldIndex = 0,
|
||||||
|
newIndex = 0;
|
||||||
|
|
||||||
|
while (oldIndex < oldStr.length || newIndex < newStr.length) {
|
||||||
|
if (oldStr[oldIndex] !== newStr[newIndex]) {
|
||||||
|
let start = oldIndex;
|
||||||
|
|
||||||
|
// Identify the changed portion
|
||||||
|
while (oldIndex < oldStr.length && oldStr[oldIndex] !== newStr[newIndex]) {
|
||||||
|
oldIndex++;
|
||||||
|
}
|
||||||
|
while (newIndex < newStr.length && newStr[newIndex] !== oldStr[start]) {
|
||||||
|
newIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
changes.push({
|
||||||
|
from: start,
|
||||||
|
to: oldIndex, // Replace the differing part
|
||||||
|
insert: newStr.substring(start, newIndex)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
oldIndex++;
|
||||||
|
newIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changes;
|
||||||
|
}
|
||||||
|
|
||||||
export let id = '';
|
export let id = '';
|
||||||
export let lang = '';
|
export let lang = '';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user