Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A JSON Canvas (`.canvas`) renderer with three layers on one document model:
- `src/core/`: spec-pure JSON Canvas (parser, serialiser, spatial index, state, colour presets). No React, no rendering, and no community conventions such as cssclasses or callouts.
- `src/renderer/`: the React Native renderer (Skia + Reanimated) for iOS, Android and macOS, consumed from the package root.
- `src/gtk/`: the GTK4 / libadwaita renderer (Cairo, through GTKX) for Linux, consumed as `./gtk`.
- `src/menu/`: a card's long-press menu on Android (Material 3's dropdown, from `@expo/ui`'s Compose components), consumed as `./menu`. It's its own entry point so the package root never imports `@expo/ui`, an optional peer: nothing under `src/renderer/` or `src/index.ts` may import from `src/menu/`.

Workspace consumes all three. Issue numbers written `workspace#N` are in workspace-sh/workspace.

Expand Down
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ the library ships every gesture it supports as a default.
| Ctrl + scroll to zoom | — | — | — | ✓ (about the pointer) |
| Double-tap to zoom-to-node | ✓ | ✓ | ✓ (trackpad two-finger double-tap, a.k.a. Smart Zoom) | — |
| Inertial fling | ✓ | ✓ | — (macOS pan is direct, no inertia) | — |
| Long-press a card | ✓ (`onNodeLongPress`) | ✓ (`onNodeLongPress`) | — (right-click: `onNodeContextMenu`) | — |

### iOS / Android (Expo or bare RN)

Expand All @@ -83,6 +84,46 @@ Expo: works in any SDK that supports the peer-dep range. Use
`expo install` if you prefer Expo's version-pinning. No native config
needed in `app.json`.

### A card's menu

A long-press on a card calls `onNodeLongPress(node, press)`, with where the
card is drawn (`press.frame`). The actions are the host's; what shows them
is the platform's:

- **iOS:** the system's context menu, with `CanvasNodePreview` lifted from
`press.frame` (Workspace uses `react-native-ios-context-menu`).
- **Android:** `CanvasNodeMenu`, from its own entry point, is Material 3's
dropdown menu (Jetpack Compose, through
[Expo UI](https://docs.expo.dev/versions/latest/sdk/ui/)) anchored to the
card: below it, or above it with no room below, lined up with its leading
edge, and the card outlined while the menu is open. It needs `@expo/ui`
(an optional peer, so the package root doesn't); icons are Material
Symbols' vector drawables, from `@expo/material-symbols`. Elsewhere it
draws nothing.

```tsx
import {CanvasNodeMenu} from '@workspace.sh/react-native-jsoncanvas/menu';

const [pressed, setPressed] = useState<{node: CanvasNode; press: NodeLongPress} | null>(null);

<CanvasView content={json} onNodeLongPress={(node, press) => setPressed({node, press})} />
{/* Mounted per press here; one kept-mounted instance works too: each new
`press` opens it afresh and is dismissed once. */}
{pressed && (
<CanvasNodeMenu
node={pressed.node}
press={pressed.press}
items={[{key: 'copy', title: 'Copy text', icon: require('@expo/material-symbols/content_copy.xml')}]}
onSelect={key => run(key, pressed.node)}
onDismiss={() => setPressed(null)}
/>
)}
```

Render it beside the `CanvasView`, in the same coordinates. The example app
(`example/expo-app/nodeMenu.tsx`) offers what Workspace does: copy, share
and open.

### macOS (`react-native-macos`)

RNGH on macOS can't see trackpad multi-finger gestures
Expand Down Expand Up @@ -211,7 +252,7 @@ consumer (Workspace's Linux client). See `AGENTS.md`.
Example harnesses live under `example/` and follow the org's standard layout
(mirrors `react-native-source-editor/example/*`):

- **`example/expo-app/`** — Expo SDK 55 host serving iOS + Android. Uses Expo
- **`example/expo-app/`** — Expo SDK 58 host serving iOS + Android. Uses Expo
CNG, owned by `expo prebuild` — never run `pod install` (iOS) or hand-edit
`android/` here manually.
- **`example/macos-app/`** — `react-native-macos` 0.81 host. Tracked in #21.
Expand Down
4 changes: 4 additions & 0 deletions example/expo-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ yarn-error.*
# generated native folders
/ios
/android

# Native (Expo CNG: generated by prebuild)
/ios
/android
28 changes: 24 additions & 4 deletions example/expo-app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {useCallback, useRef, useState} from 'react';
import {Pressable, StyleSheet, Text, useColorScheme, View} from 'react-native';
import {GestureHandlerRootView} from 'react-native-gesture-handler';
import {SafeAreaProvider, useSafeAreaInsets} from 'react-native-safe-area-context';
import {StatusBar} from 'expo-status-bar';
import * as DocumentPicker from 'expo-document-picker';
import * as FileSystem from 'expo-file-system';
import {CanvasView} from '@workspace.sh/react-native-jsoncanvas';
import {SAMPLE_CANVAS} from '../shared/fixtures';
import {useNodeMenu} from './nodeMenu';

type Controls = {
fitToViewport: (leftInset?: number) => void;
Expand All @@ -18,7 +20,22 @@ function dirname(uri: string): string {
return i >= 0 ? uri.slice(0, i + 1) : uri;
}

/** The buttons' row: its height, and the gap kept below it and above the minimap. */
const CONTROLS_HEIGHT = 40;
const GAP = 16;

export default function App() {
return (
<SafeAreaProvider>
<Playground />
</SafeAreaProvider>
);
}

function Playground() {
// The canvas draws edge to edge (Android draws behind its system bars);
// the buttons and the minimap keep inside the safe area.
const insets = useSafeAreaInsets();
const controlsRef = useRef<Controls | null>(null);
const [lastAction, setLastAction] = useState<string>('—');
// Active canvas content + basePath for the renderer. Defaults to the
Expand All @@ -30,6 +47,7 @@ export default function App() {
// wrapper has to match so canvas-empty background doesn't fight node
// colours (e.g. dark nodes on a white background in dark mode).
const isDark = useColorScheme() === 'dark';
const nodeMenu = useNodeMenu(basePath);

const openFile = useCallback(async () => {
try {
Expand Down Expand Up @@ -73,8 +91,12 @@ export default function App() {
onReady={c => {
controlsRef.current = c;
}}
onNodeLongPress={nodeMenu.onNodeLongPress}
safeAreaInsets={insets}
minimapBottomInset={CONTROLS_HEIGHT + GAP}
/>
<View style={styles.controls} pointerEvents="box-none">
{nodeMenu.menu}
<View style={[styles.controls, {bottom: insets.bottom + GAP, left: insets.left + GAP, right: insets.right + GAP}]} pointerEvents="box-none">
<Pressable
style={({pressed}) => [styles.button, pressed && styles.pressed]}
onPress={openFile}
Expand Down Expand Up @@ -117,9 +139,7 @@ const styles = StyleSheet.create({
root: {flex: 1},
controls: {
position: 'absolute',
bottom: 32,
left: 16,
right: 16,
height: CONTROLS_HEIGHT,
flexDirection: 'row',
gap: 8,
alignItems: 'center',
Expand Down
1 change: 1 addition & 0 deletions example/expo-app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"bundleIdentifier": "com.leslieoa.playground"
},
"android": {
"package": "com.leslieoa.playground",
"adaptiveIcon": {
"backgroundColor": "#E6F4FE",
"foregroundImage": "./assets/android-icon-foreground.png",
Expand Down
96 changes: 96 additions & 0 deletions example/expo-app/nodeMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// A card's long-press menu, as Workspace offers it: copy, share, open. On
// Android it's the library's CanvasNodeMenu (Material's dropdown, anchored
// to the card); elsewhere the playground keeps the canvas's own long-press,
// which reveals an image's name.
import {useCallback, useState, type ReactElement} from 'react';
import {Linking, Platform, Share} from 'react-native';
import * as Clipboard from 'expo-clipboard';
import {AndroidHaptics, performAndroidHapticsAsync} from 'expo-haptics';
import {resolveFileUri, type CanvasNode, type NodeLongPress} from '@workspace.sh/react-native-jsoncanvas';
import {CanvasNodeMenu, type CanvasNodeMenuItem} from '@workspace.sh/react-native-jsoncanvas/menu';

interface Action {
item: CanvasNodeMenuItem;
run: () => void;
}

const ICONS = {
copy: require('@expo/material-symbols/content_copy.xml'),
share: require('@expo/material-symbols/share.xml'),
open: require('@expo/material-symbols/open_in_browser.xml'),
link: require('@expo/material-symbols/link.xml'),
};

/** The menu's title: what the card is, where the card doesn't say it in a line. */
function titleOf(node: CanvasNode): string | undefined {
switch (node.type) {
case 'file':
return node.file.split('/').pop() ?? node.file;
case 'link':
try {
return new URL(node.url).host;
} catch {
return node.url;
}
default:
return undefined;
}
}

function actionsOf(node: CanvasNode, basePath: string | undefined): Action[] {
const share = (content: {url: string} | {message: string}): Action => ({
item: {key: 'share', title: 'Share…', icon: ICONS.share},
// Android's share sheet takes text, so a link goes as its message.
run: () => void Share.share('url' in content ? {message: content.url} : content),
});
switch (node.type) {
case 'text':
return [
{item: {key: 'copy', title: 'Copy text', icon: ICONS.copy}, run: () => void Clipboard.setStringAsync(node.text)},
share({message: node.text}),
];
case 'link':
return [
{item: {key: 'open', title: 'Open link', icon: ICONS.open}, run: () => void Linking.openURL(node.url)},
{item: {key: 'copy', title: 'Copy link', icon: ICONS.link}, run: () => void Clipboard.setStringAsync(node.url)},
share({url: node.url}),
];
case 'file': {
const uri = resolveFileUri(node.file, basePath);
return uri ? [share({url: uri})] : [];
}
default:
return [];
}
}

export function useNodeMenu(basePath: string | undefined): {
onNodeLongPress: ((node: CanvasNode, press: NodeLongPress) => void) | undefined;
menu: ReactElement | null;
} {
const [pressed, setPressed] = useState<{node: CanvasNode; press: NodeLongPress} | null>(null);
const onNodeLongPress = useCallback(
(node: CanvasNode, press: NodeLongPress) => {
// Nothing to offer (a group), no menu.
if (actionsOf(node, basePath).length === 0) return;
void performAndroidHapticsAsync(AndroidHaptics.Long_Press).catch(() => {});
setPressed({node, press});
},
[basePath],
);
if (Platform.OS !== 'android') return {onNodeLongPress: undefined, menu: null};
if (!pressed) return {onNodeLongPress, menu: null};
const actions = actionsOf(pressed.node, basePath);
const menu = (
<CanvasNodeMenu
key={`${pressed.node.id}:${pressed.press.at.x}:${pressed.press.at.y}`}
node={pressed.node}
press={pressed.press}
title={titleOf(pressed.node)}
items={actions.map(a => a.item)}
onSelect={key => actions.find(a => a.item.key === key)?.run()}
onDismiss={() => setPressed(null)}
/>
);
return {onNodeLongPress, menu};
}
5 changes: 5 additions & 0 deletions example/expo-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
"main": "index.ts",
"private": true,
"dependencies": {
"@expo/material-symbols": "^0.1.1",
"@expo/ui": "~58.0.27",
"@workspace.sh/react-native-jsoncanvas": "file:../..",
"expo": "^58.0.22",
"expo-clipboard": "~58.0.4",
"expo-dev-client": "~58.0.18",
"expo-document-picker": "~58.0.4",
"expo-file-system": "~58.0.13",
"expo-haptics": "~58.0.7",
"expo-status-bar": "~58.0.4",
"expo-system-ui": "~58.0.7",
"react": "19.3.0",
"react-native": "0.88.1",
"react-native-gesture-handler": "~3.3.0",
Expand Down
Loading
Loading