Skip to content

Commit

Permalink
Save document through Yjs websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Dec 1, 2021
1 parent e5cb6c4 commit bea8a1d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import errno
import json
import mimetypes
import os
import shutil
Expand Down Expand Up @@ -40,6 +41,12 @@
# windows + py2
from jupyter_server.utils import samefile_simple as samefile

try:
from jupyterlab.handlers.yjs_echo_ws import ROOMS
YJS_SUPPORT = True
except ImportError:
YJS_SUPPORT = False

_script_exporter = None


Expand Down Expand Up @@ -435,7 +442,7 @@ def save(self, model, path=""):

if "type" not in model:
raise web.HTTPError(400, u"No file type provided")
if "content" not in model and model["type"] != "directory":
if not YJS_SUPPORT and "content" not in model and model["type"] != "directory":
raise web.HTTPError(400, u"No file content provided")

os_path = self._get_os_path(path)
Expand All @@ -445,15 +452,25 @@ def save(self, model, path=""):

try:
if model["type"] == "notebook":
nb = nbformat.from_dict(model["content"])
if YJS_SUPPORT:
nb_dict = json.loads(ROOMS[path].source)
model["content"] = nb_dict
else:
nb_dict = model["content"]
nb = nbformat.from_dict(nb_dict)
self.check_and_sign(nb, path)
self._save_notebook(os_path, nb)
# One checkpoint should always exist for notebooks.
if not self.checkpoints.list_checkpoints(path):
self.create_checkpoint(path)
elif model["type"] == "file":
# Missing format will be handled internally by _save_file.
self._save_file(os_path, model["content"], model.get("format"))
if YJS_SUPPORT:
file_str = ROOMS[path].source
model["content"] = file_str
else:
file_str = model["content"]
self._save_file(os_path, file_str, model.get("format"))
elif model["type"] == "directory":
self._save_directory(os_path, model, path)
else:
Expand Down

0 comments on commit bea8a1d

Please sign in to comment.