diff --git a/README.md b/README.md index 0052c0f..5acdac8 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![npm](https://img.shields.io/npm/v/react-native-codepad?style=flat-square)](https://www.npmjs.com/package/react-native-codepad) [![license](https://img.shields.io/npm/l/react-native-codepad?style=flat-square&label=license)](https://github.com/SectionTN/react-native-codepad/blob/main/LICENSE) -A native code editor for React Native. It runs one C++ core on both platforms, drawn by a native view in Kotlin on Android and Swift on iOS, with tree-sitter highlighting and no WebView. +A native code editor for React Native. It runs one C++ core on Android, iOS and the web, drawn by a native view in Kotlin on Android and Swift on iOS and by the page in the browser, with tree-sitter highlighting and no WebView. diff --git a/website/docs/guides/web.md b/website/docs/guides/web.md new file mode 100644 index 0000000..4f894dd --- /dev/null +++ b/website/docs/guides/web.md @@ -0,0 +1,92 @@ +--- +title: Web +description: Run CodePad in the browser with Expo web, serve its files, and see what works differently there. +sidebar_position: 9 +--- + +CodePad runs in the browser through React Native Web, with the same `CodeEditor` props and ref as on Android and iOS. + +The browser runs the same C++ core, compiled to WebAssembly. CodePad draws its own caret, selection handles and edit menu there too. + +## Set up Expo web + +Install the packages Expo needs for the web, if your app doesn't run there yet: + +```sh +npx expo install react-dom react-native-web @expo/metro-runtime +``` + +## Copy the files + +The browser loads the core and the languages while the app runs, from your app's `public` folder. Run the `web` command from your app's root to copy them there: + +```sh +npx react-native-codepad web public +``` + +It copies the core and every installed language package to `public/codepad/`, including the custom languages in `modules/`. Run it again after you add or remove a language or update CodePad. Pass `--root ` to run it from another folder. + +Expo serves `public/` at the root of the site and copies it into `dist/` on export, so run the command before both: + +```json title="package.json" +{ + "scripts": { + "web": "react-native-codepad web public && expo start --web", + "export:web": "react-native-codepad web public && expo export --platform web" + } +} +``` + +Add `public/codepad/` to `.gitignore`, because the command writes it again from the installed packages. + +When a file is missing, the editor is blank or shows plain text, and the browser console shows the address of the file it couldn't load. + +## Serve the files from another path + +The editor loads its files from `/codepad/`. When they live somewhere else, such as a CDN or a sub path, call `configureWeb` once before the first editor mounts: + +```tsx +import { configureWeb } from 'react-native-codepad'; + +configureWeb({ assetsUrl: 'https://cdn.example.com/codepad/' }); +``` + +`configureWeb` does nothing on Android and iOS, so it can stay in shared code. + +## Languages + +The `language` prop works as it does on Android and iOS. The editor loads a language the first time an editor uses it. A language inside another one, such as JavaScript in HTML, loads when the text first needs it. + +`Codepad.availableLanguages()` returns an empty list on the web until an editor has loaded its first language. + +The `language` command also builds the web version of a [custom language](/guides/custom-languages/). It uses tree-sitter's own command-line tool, which downloads its compiler the first time, and it needs `grammar.js` next to `src/` in the grammar folder. When the web build fails, the command prints a warning and still writes the package for Android and iOS. The `web` command then stops on that package until it has a web build. Pass `--no-web` only when your app doesn't run on the web. + +## Selection + +With a mouse: + +- Drag to select. Hold the drag past the editor's edge to scroll. +- Double-click to select a word, or triple-click to select a line with its line break. Dragging after either extends the selection by words or by lines. +- Shift+click to extend the selection from where it started. +- Right-click to open the edit menu. + +With touch, the editor follows Android: + +- Tap to place the caret. A handle shows under it for 4 seconds. +- Double-tap to select a word. +- Touch and hold to select a word and open the edit menu. Move your finger without lifting it to extend the selection by words. +- Drag a handle to move that end of the selection. The ends can cross each other. +- Tap the caret's handle, or tap inside the selection, to open the edit menu. + +The handles show only after a touch or pen press, never after a mouse press. They take the theme's `cursor` color. + +The edit menu has Cut, Copy, Paste and Select all. Cut and Copy need selected text. Cut and Paste are hidden when `readOnly` is on, and Paste is also hidden when the browser doesn't let the page read the clipboard. + +## Limits + +- The ref's `copy()`, `cut()` and `paste()` and the menu's Paste need a secure origin, which is `https` or `localhost`. They reject when the browser refuses. The menu's Cut and Copy also work over plain `http`. +- On iOS, Safari shows its form bar above the keyboard while the editor has the focus. +- The menu labels are in English. +- Screen readers read the editor's text as page text. They don't announce the caret or your edits. +- CodePad runs on the page's main thread, with no web worker. +- The web tests run in Chromium and WebKit, the engines of Chrome and Safari. Firefox is untested. diff --git a/website/docs/internals/roadmap.md b/website/docs/internals/roadmap.md index b0a093c..034685b 100644 --- a/website/docs/internals/roadmap.md +++ b/website/docs/internals/roadmap.md @@ -10,6 +10,8 @@ CodePad 0.1 is on npm, with the core package and nineteen language packages rele Both editors are complete, and they run on one C++ core shared by Android and iOS. Android is tested on the JVM and on an emulator, and its numbers are in the [performance guide](/guides/performance/). iOS runs the same shared tests under `swift test` and XCTest on the simulator. Numbers from a physical iPhone are still missing. +The [web editor](/guides/web/) runs the same core in the browser and ships with version 0.2. + ## Before 1.0 The work planned before `1.0.0`, in order: @@ -17,7 +19,6 @@ The work planned before `1.0.0`, in order: 1. [Autocomplete](https://github.com/SectionTN/react-native-codepad/issues/3) with provider hooks, and an [LSP client](https://github.com/SectionTN/react-native-codepad/issues/6) on top of it. 2. [Code folding](https://github.com/SectionTN/react-native-codepad/issues/5) and a [minimap](https://github.com/SectionTN/react-native-codepad/issues/7). 3. [Multiple cursors](https://github.com/SectionTN/react-native-codepad/issues/8), [diagnostics](https://github.com/SectionTN/react-native-codepad/issues/4), [font ligatures](https://github.com/SectionTN/react-native-codepad/issues/9) and [correct bidi editing](https://github.com/SectionTN/react-native-codepad/issues/10). [Decorations](/guides/decorations/) already draw the underlines that diagnostics need. Until then, the arrow keys move through right-to-left text in the order it is stored, not in the order it shows on screen. -4. A [web target](https://github.com/SectionTN/react-native-codepad/issues/11). `1.0.0` is the first stable release, with this list done on both platforms. diff --git a/website/docs/reference/types.md b/website/docs/reference/types.md index b728c17..38265b6 100644 --- a/website/docs/reference/types.md +++ b/website/docs/reference/types.md @@ -32,3 +32,5 @@ sidebar_position: 4 Colors take `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()`. A theme color that doesn't parse paints magenta on Android and logs a warning. On iOS an editor color that doesn't parse takes the matching `githubLight` color, a token color takes the `githubLight` text color, and nothing is logged. A decoration whose color doesn't parse is dropped on both platforms. The `Codepad` object has one method, `availableLanguages(): string[]`, which returns the language ids installed in the app. + +`configureWeb({ assetsUrl }: { assetsUrl?: string }): void` sets the address the web editor loads its files from, `/codepad/` by default. It does nothing on Android and iOS. The [web guide](/guides/web/) explains when to call it.