Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Add HTML render/code toggle for code blocks (#1781)

Jacob Beckermancbfda9e7c8ae
diff --git a/js/app/packages/block-code/component/Block.tsx b/js/app/packages/block-code/component/Block.tsx
index 1a51116471..957aa7ee97 100644
--- a/js/app/packages/block-code/component/Block.tsx
+++ b/js/app/packages/block-code/component/Block.tsx
@@ -1,21 +1,43 @@
import { useIsNestedBlock } from '@core/block';
import { DocumentBlockContainer } from '@core/component/DocumentBlockContainer';
-import { Show } from 'solid-js';
+import { blockMetadataSignal } from '@core/signal/load';
+import { createEffect, createMemo, createSignal, on, Show } from 'solid-js';
+import { isHtmlFileType } from '../util/fileMode';
import { CodeMarkdown } from './CodeMarkdown';
import { CodeMirror } from './CodeMirror';
+import { HtmlPreview } from './HtmlPreview';
import { ModalsProvider } from './ModalsProvider';
import { TopBar } from './TopBar';
+export type CodeBlockMode = 'code' | 'render';
+
export default function BlockCode() {
const isNestedBlock = useIsNestedBlock();
+ const blockMetadata = blockMetadataSignal.get;
+ const isHtmlFile = createMemo(() =>
+ isHtmlFileType(blockMetadata()?.fileType)
+ );
+ const [mode, setMode] = createSignal<CodeBlockMode>('code');
+
+ createEffect(
+ on(isHtmlFile, (htmlFile) => {
+ setMode(htmlFile ? 'render' : 'code');
+ })
+ );
return (
<DocumentBlockContainer usesCenterBar>
<Show when={!isNestedBlock} fallback={<CodeMarkdown />}>
<div class="size-full bg-panel select-none overscroll-none overflow-hidden flex flex-col items-end relative">
<ModalsProvider>
- <TopBar />
- <CodeMirror />
+ <TopBar
+ isHtmlFile={isHtmlFile()}
+ mode={mode()}
+ onModeChange={setMode}
+ />
+ <Show when={mode() === 'render'} fallback={<CodeMirror />}>
+ <HtmlPreview />
+ </Show>
</ModalsProvider>
</div>
</Show>
diff --git a/js/app/packages/block-code/component/HtmlPreview.tsx b/js/app/packages/block-code/component/HtmlPreview.tsx
new file mode 100644
index 0000000000..2232f1f8e7
--- /dev/null
+++ b/js/app/packages/block-code/component/HtmlPreview.tsx
@@ -0,0 +1,17 @@
+import { blockTextSignal } from '@core/signal/load';
+
+export function HtmlPreview() {
+ const blockText = blockTextSignal.get;
+
+ return (
+ <div class="size-full bg-panel overflow-auto">
+ <iframe
+ title="HTML preview"
+ class="size-full border-0 bg-white"
+ sandbox="allow-scripts"
+ referrerPolicy="no-referrer"
+ srcdoc={blockText() ?? ''}
+ />
+ </div>
+ );
+}
diff --git a/js/app/packages/block-code/component/TopBar.tsx b/js/app/packages/block-code/component/TopBar.tsx
index 7d4a4d2b3d..aa32c8e59e 100644
--- a/js/app/packages/block-code/component/TopBar.tsx
+++ b/js/app/packages/block-code/component/TopBar.tsx
@@ -1,4 +1,5 @@
import { useDrawerControl } from '@app/component/split-layout/components/SplitDrawerContext';
+import { SplitToolbarRight } from '@app/component/split-layout/components/SplitToolbar';
import type { BlockTool } from '@app/component/ResponsiveBlockToolbar';
import {
ResponsiveBlockToolbar,
@@ -14,6 +15,7 @@ import {
DocumentPropertiesButton,
PROPERTIES_DRAWER_ID,
} from '@core/component/DocumentPropertiesModal';
+import { SegmentedControl } from '@core/component/FormControls/SegmentControls';
import {
ReferencesButton,
REFERENCES_DRAWER_ID,
@@ -22,6 +24,7 @@ import {
ShareTrigger,
useShareDialogContext,
} from '@core/component/TopBar/ShareButton';
+import { isMobile } from '@core/mobile/isMobile';
import { blockTextSignal } from '@core/signal/load';
import {
useBlockDocumentDownloadName,
@@ -34,10 +37,16 @@ import IconShared from '@icon/regular/share.svg';
import TagIcon from '@icon/regular/tag.svg';
import { createCallback } from '@solid-primitives/rootless';
import type { Component } from 'solid-js';
+import { Show } from 'solid-js';
+import type { CodeBlockMode } from './Block';
const { track, TrackingEvents } = withAnalytics();
-export const TopBar: Component = () => {
+export const TopBar: Component<{
+ isHtmlFile: boolean;
+ mode: CodeBlockMode;
+ onModeChange: (mode: CodeBlockMode) => void;
+}> = (props) => {
const blockId = useBlockId();
const text = blockTextSignal.get;
const name = useBlockDocumentName();
@@ -104,6 +113,20 @@ export const TopBar: Component = () => {
<ResponsivePermissionsBadge />
+ <Show when={props.isHtmlFile && !isMobile()}>
+ <SplitToolbarRight order={-1}>
+ <SegmentedControl
+ list={[
+ { value: 'render', label: 'Render' },
+ { value: 'code', label: 'Code' },
+ ]}
+ size="SM"
+ value={props.mode}
+ onChange={(value) => props.onModeChange(value as CodeBlockMode)}
+ />
+ </SplitToolbarRight>
+ </Show>
+
<ResponsiveBlockToolbar
tools={tools}
ops={ops}
diff --git a/js/app/packages/block-code/util/fileMode.test.ts b/js/app/packages/block-code/util/fileMode.test.ts
new file mode 100644
index 0000000000..bd7a32dfea
--- /dev/null
+++ b/js/app/packages/block-code/util/fileMode.test.ts
@@ -0,0 +1,18 @@
+import { describe, expect, it } from 'vitest';
+import { isHtmlFileType } from './fileMode';
+
+describe('isHtmlFileType', () => {
+ it('returns true for supported html file extensions', () => {
+ expect(isHtmlFileType('html')).toBe(true);
+ expect(isHtmlFileType('HTM')).toBe(true);
+ expect(isHtmlFileType('xhtml')).toBe(true);
+ expect(isHtmlFileType('shtml')).toBe(true);
+ });
+
+ it('returns false for unsupported extensions', () => {
+ expect(isHtmlFileType('md')).toBe(false);
+ expect(isHtmlFileType('js')).toBe(false);
+ expect(isHtmlFileType(undefined)).toBe(false);
+ expect(isHtmlFileType(null)).toBe(false);
+ });
+});
diff --git a/js/app/packages/block-code/util/fileMode.ts b/js/app/packages/block-code/util/fileMode.ts
new file mode 100644
index 0000000000..c3eee6f7b4
--- /dev/null
+++ b/js/app/packages/block-code/util/fileMode.ts
@@ -0,0 +1,6 @@
+const HTML_FILE_TYPES = new Set(['html', 'htm', 'xhtml', 'shtml']);
+
+export function isHtmlFileType(fileType?: string | null) {
+ if (!fileType) return false;
+ return HTML_FILE_TYPES.has(fileType.toLowerCase());
+}