Skip to content

Commit

Permalink
Fix startFileEdit (#181)
Browse files Browse the repository at this point in the history
* Fix remote file edit open

* Fix remote file edit save
  • Loading branch information
pertsevpv authored Feb 13, 2025
1 parent 2df8ec6 commit e2722b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.sudu.experiments.diff.JsViewController;
import org.sudu.experiments.editor.*;
import org.sudu.experiments.js.*;
import org.sudu.experiments.protocol.JsCast;
import org.teavm.jso.JSObject;
import org.teavm.jso.core.JSString;

Expand All @@ -27,6 +28,24 @@ public JsRemoteEditor(
editor = demoEdit0().editor();
if (args.hasTheme()) setTheme(args.getTheme());
controller = new JsEditorViewController0();
channel.setOnMessage(this::onMessage);
editor.setOnDiffMadeListener((_1, _2, _3) -> onEdit());
}

private void onMessage(JsArray<JSObject> jsArray) {
String source = JsCast.string(jsArray, 0);
String encoding = JsCast.string(jsArray, 1);
String name = JsCast.string(jsArray, 2);
editor.openFile(source, encoding, name);
}

private void onEdit() {
JSString source = JsCast.jsString(editor.model().document.makeString());
JSString encoding = JSString.valueOf(editor.model().encoding());
JsArray<JSObject> jsArray = JsArray.create();
jsArray.set(0, source);
jsArray.set(1, encoding);
channel.sendMessage(jsArray);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.sudu.experiments.encoding.JsTextFileReader;
import org.sudu.experiments.js.JsArray;
import org.sudu.experiments.js.JsHelper;
import org.sudu.experiments.protocol.JsCast;
import org.teavm.jso.JSObject;
import org.teavm.jso.core.JSString;

Expand All @@ -17,13 +18,20 @@ public class FileEditChannelUpdater {

public FileEditChannelUpdater(Channel channel) {
this.channel = channel;
this.channel.setOnMessage(this::onMessage);
}

public void setFile(FileHandle fileHandle) {
this.handle = fileHandle;
JsTextFileReader.read(handle, this::sendMessage, this::onError);
}

public void onMessage(JsArray<JSObject> jsArray) {
String source = JsCast.string(jsArray, 0);
String encoding = JsCast.string(jsArray, 1);
handle.writeText(source, encoding, () -> {}, this::onError);
}

public void sendMessage(JSString source, JSString encoding) {
if (debug) LoggingJs.debug(JsHelper.concat(
"FileEditChannelUpdater.sendMessage, length = " + source.getLength()
Expand Down

0 comments on commit e2722b4

Please sign in to comment.