Skip to content
Open
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
10 changes: 8 additions & 2 deletions example/src/EditorApp.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { NESTED_SNIPPET } from './screens/nestedSnippet';
import { THEMES } from './themes';

type Demo = 'editor' | 'nested' | 'two';
const FILES = [...SAMPLE_FILES, ...GENERATED_FILES.filter((f) => f.id === 'js10k')];
const FILES = [
...SAMPLE_FILES,
...GENERATED_FILES.filter((f) => f.id === 'js10k' || f.id === 'min'),
];
const FONT = 'JetBrainsMono_400Regular';
const NO_DECORATIONS: { items: Decoration[]; revision: number } = { items: [], revision: -1 };

Expand All @@ -29,11 +32,12 @@ function Chip({ label, on, onPress }: { label: string; on?: boolean; onPress: ()
);
}

// The example on the web: a file picker, a theme and scrollEnabled toggle, a nested and a two editor demo.
// The example on the web: a file picker, theme, scrollEnabled and readOnly toggles, a nested and a two editor demo.
export function EditorApp() {
const [fileId, setFileId] = useState('python');
const [themeIndex, setThemeIndex] = useState(0);
const [scrollEnabled, setScrollEnabled] = useState(true);
const [readOnly, setReadOnly] = useState(false);
const [suggestions, setSuggestions] = useState<'on' | 'off'>('off');
const [decorate, setDecorate] = useState(false);
const [finding, setFinding] = useState(false);
Expand Down Expand Up @@ -113,6 +117,7 @@ export function EditorApp() {
on={!scrollEnabled}
onPress={() => setScrollEnabled((s) => !s)}
/>
<Chip label="readOnly" on={readOnly} onPress={() => setReadOnly((r) => !r)} />
<Chip
label={`suggestions ${suggestions}`}
on={suggestions === 'on'}
Expand Down Expand Up @@ -169,6 +174,7 @@ export function EditorApp() {
theme={theme}
fontFamily={FONT}
scrollEnabled={scrollEnabled}
readOnly={readOnly}
suggestions={suggestions}
avoidKeyboard
onReady={ready(benchLines ? `bench ${benchLines}` : (file?.name ?? ''))}
Expand Down
9 changes: 9 additions & 0 deletions example/src/bench/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export function generateJavaScript(lines: number): string {
return out.join('\n');
}

/** generateJavaScript on one line without its comments, as a minifier leaves it. */
export function generateMinified(lines: number): string {
return generateJavaScript(lines)
.split('\n')
.filter((line) => !line.startsWith('//'))
.map((line) => line.trim())
.join('');
}

const WORDS = ['editor', 'caret', 'buffer', 'syntax', 'theme', 'inset', 'scroll', 'layout'];

/** Deterministic markdown: a heading every 50 paragraphs, emphasis and inline code in each, a fenced block every tenth. */
Expand Down
8 changes: 7 additions & 1 deletion example/src/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateJavaScript, generateMarkdown } from './bench/generate';
import { generateJavaScript, generateMarkdown, generateMinified } from './bench/generate';
import { type LanguageId, SAMPLES } from './samples';

export type EditorFile = {
Expand Down Expand Up @@ -85,6 +85,12 @@ export const GENERATED_FILES: EditorFile[] = [
language: 'markdown',
load: () => generateMarkdown(2000),
},
{
id: 'min',
name: 'minified.js',
language: 'javascript',
load: () => generateMinified(1000),
},
];

export const UNTITLED: EditorFile = { id: 'untitled', name: 'untitled', load: () => '' };
Expand Down
1 change: 1 addition & 0 deletions scripts/bench-web.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ try {
if (desktop !== null) await cdp.send('Emulation.setCPUThrottlingRate', { rate: 1 });
const probe = await page.evaluate(() => globalThis.__codepadProbe);
const stats = (list) => {
if (list.length === 0) return 'none';
const sorted = [...list].sort((a, b) => a - b);
const mean = list.reduce((a, b) => a + b, 0) / Math.max(1, list.length);
return `mean ${ms(mean)}, p50 ${ms(pct(sorted, 0.5))}, p95 ${ms(pct(sorted, 0.95))}, max ${ms(sorted.at(-1) ?? 0)}`;
Expand Down
2 changes: 2 additions & 0 deletions src/web/EditMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export class EditMenu {
const { width, height } = this.size;
let top = anchor.y - GAP - height;
if (top < visible.y) top = anchor.y + anchor.height + GAP;
// With room neither above nor below, the menu sits as high as it can inside the visible area, as Android's toolbar.
if (top + height > visible.y + visible.height) top = visible.y;
const centre = anchor.x + anchor.width / 2;
const left = Math.max(
visible.x,
Expand Down
Loading