Skip to content

Commit

Permalink
feat: add language support
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Mar 1, 2023
1 parent a616e1a commit 273c4d3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
1 change: 1 addition & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VITE_WS_URL="ws://localhost:1234/ws"
VITE_API_URL="http://localhost:8080"
VITE_LANGUAGE="en"
13 changes: 9 additions & 4 deletions client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ body {
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}

a {
color: inherit;
text-decoration: none;
}

* {
box-sizing: border-box;
}

.tldraw {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
width: 100%;
height: 100%;
}
4 changes: 3 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Editor from "./components/Editor";
import "./App.css";

const { VITE_LANGUAGE } = import.meta.env;

export default function App() {
const urlParams = window.location.pathname.slice(1).split("/");
const roomId = urlParams[0] === "r" ? urlParams[1] : undefined;

return (
<div className="tldraw">
<Editor roomId={roomId} />
<Editor roomId={roomId} language={VITE_LANGUAGE} />
</div>
);
}
34 changes: 18 additions & 16 deletions client/src/components/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
import { Tldraw, TldrawProps, useFileSystem } from "@tldraw/tldraw";
import { Tldraw, TldrawApp, useFileSystem } from "@tldraw/tldraw";
import { useAssets } from "../hooks/useAssets";
import { useMultiplayer } from "../hooks/useMultiplayer";
import { initProvider } from "../utils/y";
import { CustomCursor } from "./Cursor";
import PropTypes from "prop-types";

interface Props {
roomId: string | undefined;
readOnly?: boolean;
}
import { Default, Multiplayer, Settings } from "../types/types";

Editor.propTypes = {
roomId: PropTypes.string,
readOnly: PropTypes.string,
language: PropTypes.string,
};

const components = {
Cursor: CustomCursor,
};

function Editor({ roomId, readOnly }: Props) {
let editor = <DefaultEditor />;
function Editor({ roomId, readOnly, language }: Settings) {
language = language || "en";
let editor = <DefaultEditor language={language} />;
if (roomId) {
initProvider(roomId);
editor = readOnly ? (
<MultiplayerReadOnlyEditor roomId={roomId} />
<MultiplayerReadOnlyEditor roomId={roomId} language={language} />
) : (
<MultiplayerEditor roomId={roomId} />
<MultiplayerEditor roomId={roomId} language={language} />
);
}

return editor;
}

function DefaultEditor({ ...rest }: Partial<TldrawProps>) {
function DefaultEditor({ language }: Default) {
const fileSystemEvents = useFileSystem();
const { onAssetCreate, onAssetDelete, onAssetUpload } = useAssets();

const handleMount = (app: TldrawApp) => {
app.setSetting("language", language);
};

return (
<Tldraw
autofocus
onMount={handleMount}
components={components}
onAssetCreate={onAssetCreate}
onAssetDelete={onAssetDelete}
onAssetUpload={onAssetUpload}
{...fileSystemEvents}
{...rest}
/>
);
}

function MultiplayerEditor({ roomId }: Props) {
function MultiplayerEditor({ roomId, language }: Multiplayer) {
const fileSystemEvents = useFileSystem();
const { onAssetCreate, onAssetDelete, onAssetUpload } = useAssets();
const { ...events } = useMultiplayer(roomId!);
const { ...events } = useMultiplayer(roomId, language);

return (
<Tldraw
Expand All @@ -69,9 +71,9 @@ function MultiplayerEditor({ roomId }: Props) {
);
}

function MultiplayerReadOnlyEditor({ roomId }: Props) {
function MultiplayerReadOnlyEditor({ roomId, language }: Multiplayer) {
const { onSaveProjectAs, onSaveProject } = useFileSystem();
const { ...events } = useMultiplayer(roomId!);
const { ...events } = useMultiplayer(roomId, language);

return (
<Tldraw
Expand Down
3 changes: 2 additions & 1 deletion client/src/hooks/useMultiplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {

// declare const window: Window & { app: TldrawApp };

export function useMultiplayer(roomId: string) {
export function useMultiplayer(roomId: string, language: string) {
const [app, setApp] = useState<TldrawApp>();
const [loading, setLoading] = useState(true);

Expand All @@ -35,6 +35,7 @@ export function useMultiplayer(roomId: string) {
// Put the state into the window, for debugging.
const onMount = useCallback(
(app: TldrawApp) => {
app.setSetting("language", language);
app.loadRoom(roomId);
app.pause(); // Turn off the app's own undo / redo stack
// window.app = app;
Expand Down
14 changes: 14 additions & 0 deletions client/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Settings = {
roomId?: string;
readOnly?: boolean;
language?: string;
};

export type Default = {
language: string;
};

export type Multiplayer = {
roomId: string;
language: string;
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"clean": "find . -name node_modules -o -name dist -type d | xargs rm -rf",
"dev:api": "yarn workspace api dev",
"dev:client": "yarn workspace client dev",
"build:api": "yarn workspace api build",
"build:all": "yarn build:api && yarn build:client ",
"build:all": "yarn build:api && yarn build:client",
"build:all:samedir": "yarn build:all && mkdir -p dist && mv ./api/dist ./dist/api && mv ./client/dist ./dist/client",
"build:api": "yarn workspace api build",
"build:client": "yarn workspace client build",
"start:ws": "HOST=localhost PORT=1234 node ./node_modules/y-websocket/bin/server.js",
"start:api": "yarn workspace api start",
Expand Down

0 comments on commit 273c4d3

Please sign in to comment.