Skip to content

Commit

Permalink
Added option to enable/disable editable mode; default=non-editable
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-goeldi committed Sep 15, 2023
1 parent 13f6120 commit 27557d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/+c47103cf.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added option to enable/disable editable mode; default=non-editable
12 changes: 9 additions & 3 deletions src/kweb/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
from . import config
from .api.browser import router as browser_router
from .api.viewer import router as viewer_router
from .layout_server import LayoutViewServerEndpoint
from .layout_server import (
EditableLayoutViewServerEndpoint,
LayoutViewServerEndpoint,
)


def get_app(fileslocation: Path | str | None = None) -> FastAPI:
def get_app(fileslocation: Path | str | None = None, editable: bool = False) -> FastAPI:
if fileslocation is None:

def settings() -> config.Config:
Expand All @@ -36,7 +39,10 @@ def settings() -> config.Config:
viewer_router.dependencies.insert(0, Depends(settings))
browser_router.dependencies.insert(0, Depends(settings))

app.add_websocket_route("/ws", LayoutViewServerEndpoint)
if editable:
app.add_websocket_route("/ws", EditableLayoutViewServerEndpoint)
else:
app.add_websocket_route("/ws", LayoutViewServerEndpoint)
viewer_router.dependencies.insert(0, Depends(settings))
app.include_router(viewer_router)
app.include_router(browser_router)
Expand Down
12 changes: 10 additions & 2 deletions src/kweb/layout_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@


class LayoutViewServerEndpoint(WebSocketEndpoint):
encoding = "text"
editable: bool = False

def __init_subclass__(cls, editable: bool = False, **kwargs: Any):
super().__init_subclass__(**kwargs)
cls.editable = editable

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -244,7 +248,7 @@ async def send_hierarchy(self, websocket: WebSocket) -> None:
)

async def connection(self, websocket: WebSocket, path: str | None = None) -> None:
self.layout_view = lay.LayoutView(True)
self.layout_view = lay.LayoutView(self.editable)
self.layout_view.load_layout(self.url)
if Path(self.layer_props).is_file():
self.layout_view.load_layer_props(self.layer_props)
Expand Down Expand Up @@ -408,3 +412,7 @@ async def reader(self, websocket: WebSocket, data: str) -> None:
self.layout_view.zoom_fit()
case "zoom-f":
self.layout_view.zoom_fit()


class EditableLayoutViewServerEndpoint(LayoutViewServerEndpoint, editable=True):
pass
17 changes: 14 additions & 3 deletions src/kweb/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@

from . import config
from .api.viewer import router
from .layout_server import LayoutViewServerEndpoint

# from .layout_server import EditableLayoutViewServerEndpoint
from .layout_server import (
EditableLayoutViewServerEndpoint,
LayoutViewServerEndpoint,
)

def get_app(fileslocation: Path | str) -> FastAPI:

def get_app(fileslocation: Path | str, editable: bool = False) -> FastAPI:
# config.settings.fileslocation = Path(fileslocation)
def settings() -> config.Config:
return config.Config(fileslocation=fileslocation)

staticfiles = StaticFiles(directory=Path(__file__).parent / "static")

app = FastAPI(routes=[WebSocketRoute("/ws", endpoint=LayoutViewServerEndpoint)])
if editable:
app = FastAPI(
routes=[WebSocketRoute("/ws", endpoint=EditableLayoutViewServerEndpoint)]
)
else:
app = FastAPI(routes=[WebSocketRoute("/ws", endpoint=LayoutViewServerEndpoint)])

# insert the settings as the first dependency
router.dependencies.insert(0, Depends(settings))
app.include_router(router)
Expand Down

0 comments on commit 27557d4

Please sign in to comment.