🎥 Make videos programmatically with React
Elements: Highlight TSX source in install dialog (#11170)
Igor Samokhovets9e7a7dd91028
diff --git a/bun.lock b/bun.lockindex 4b965dd50c..0e5fa0b456 100644--- a/bun.lock+++ b/bun.lock@@ -1820,6 +1820,7 @@"mediabunny": "catalog:","memfs": "3.4.3","open": "8.4.2",+ "prismjs": "1.30.0","remotion": "workspace:*","semver": "7.5.3","zod": "catalog:",@@ -1828,6 +1829,7 @@"@happy-dom/global-registrator": "14.5.1","@remotion/eslint-config-internal": "workspace:*","@testing-library/react": "16.1.0",+ "@types/prismjs": "1.26.4","@types/semver": "7.5.3","@typescript/native-preview": "catalog:","eslint": "catalog:",diff --git a/packages/.monorepo/builder.ts b/packages/.monorepo/builder.tsindex 1a06693d2e..d1ffd68af3 100644--- a/packages/.monorepo/builder.ts+++ b/packages/.monorepo/builder.ts@@ -1,5 +1,5 @@-import path from 'path';import {build} from 'bun';+import path from 'path';import {Exports, validateExports} from './validate-exports';if (process.env.NODE_ENV !== 'production') {diff --git a/packages/studio/package.json b/packages/studio/package.jsonindex a4fbb63aa3..1667970f00 100644--- a/packages/studio/package.json+++ b/packages/studio/package.json@@ -24,36 +24,38 @@"react-dom": ">=16.8.0"},"dependencies": {- "@tanstack/react-virtual": "3.14.9",+ "@jridgewell/trace-mapping": "0.3.31","@remotion/canvas": "workspace:*",- "@remotion/studio-protocol": "workspace:*",- "@remotion/studio-codemods": "workspace:*",- "semver": "7.5.3",- "remotion": "workspace:*","@remotion/captions": "workspace:*","@remotion/media": "workspace:*",- "@remotion/player": "workspace:*","@remotion/media-utils": "workspace:*",+ "@remotion/player": "workspace:*","@remotion/renderer": "workspace:*",- "@remotion/web-renderer": "workspace:*",+ "@remotion/studio-codemods": "workspace:*",+ "@remotion/studio-protocol": "workspace:*","@remotion/studio-shared": "workspace:*","@remotion/timeline-utils": "workspace:*",+ "@remotion/web-renderer": "workspace:*","@remotion/zod-types": "workspace:*",- "@jridgewell/trace-mapping": "0.3.31",+ "@tanstack/react-virtual": "3.14.9","mediabunny": "catalog:","memfs": "3.4.3","open": "8.4.2",+ "prismjs": "1.30.0",+ "remotion": "workspace:*",+ "semver": "7.5.3","zod": "catalog:"},"devDependencies": {"@happy-dom/global-registrator": "14.5.1",- "react": "catalog:",- "react-dom": "catalog:",+ "@remotion/eslint-config-internal": "workspace:*","@testing-library/react": "16.1.0",+ "@types/prismjs": "1.26.4","@types/semver": "7.5.3",- "@remotion/eslint-config-internal": "workspace:*",+ "@typescript/native-preview": "catalog:","eslint": "catalog:",- "@typescript/native-preview": "catalog:"+ "react": "catalog:",+ "react-dom": "catalog:"},"publishConfig": {"access": "public"diff --git a/packages/studio/src/components/ElementInstallConfirmation.tsx b/packages/studio/src/components/ElementInstallConfirmation.tsxindex 9e3501ea11..691243c334 100644--- a/packages/studio/src/components/ElementInstallConfirmation.tsx+++ b/packages/studio/src/components/ElementInstallConfirmation.tsx@@ -1,5 +1,7 @@import {StudioProtocolInternals} from '@remotion/studio-protocol';import React, {+ lazy,+ Suspense,useCallback,useContext,useEffect,@@ -61,6 +63,10 @@ import {useResolvedStack,} from './Timeline/use-resolved-stack';+const HighlightedElementSource = lazy(+ () => import('./HighlightedElementSource'),+);+const container: React.CSSProperties = {display: 'flex',flexDirection: 'column',@@ -240,15 +246,17 @@ const sourceCodeContainerStyle: React.CSSProperties = {overflow: 'hidden',border: `1px solid ${WHITE_ALPHA_12}`,borderRadius: 6,- backgroundColor: INPUT_BACKGROUND,};const sourceCodeBlockStyle: React.CSSProperties = {+ backgroundColor: INPUT_BACKGROUND,margin: 0,+ border: 0,+ borderRadius: 0,+ maxWidth: 'none',maxHeight: 240,overflow: 'auto',padding: 12,- color: LIGHT_TEXT,fontFamily: 'monospace',fontSize: 12,lineHeight: 1.5,@@ -364,6 +372,10 @@ export const ElementInstallConfirmation: React.FC<{sourceIsUnverified,sourceLabel,} = state;+ const sourcePreview = useMemo(+ () => makeSourceControlsVisible(request.element.sourceCode),+ [request.element.sourceCode],+ );const config = Internals.useUnsafeVideoConfig();const {canvasContent, compositions} = useContext(Internals.CompositionManager,@@ -1016,11 +1028,13 @@ export const ElementInstallConfirmation: React.FC<{</summary><div style={sourceCodeContainerStyle}><pre- className={`${HORIZONTAL_SCROLLBAR_CLASSNAME} ${VERTICAL_SCROLLBAR_CLASSNAME} ${FOCUS_VISIBLE_ONLY_CLASS_NAME}`}+ className={`language-tsx ${HORIZONTAL_SCROLLBAR_CLASSNAME} ${VERTICAL_SCROLLBAR_CLASSNAME} ${FOCUS_VISIBLE_ONLY_CLASS_NAME}`}style={sourceCodeBlockStyle}><code style={sourceCodeStyle}>- {makeSourceControlsVisible(request.element.sourceCode)}+ <Suspense fallback={sourcePreview}>+ <HighlightedElementSource source={sourcePreview} />+ </Suspense></code></pre></div>diff --git a/packages/studio/src/components/HighlightedElementSource.tsx b/packages/studio/src/components/HighlightedElementSource.tsxnew file mode 100644index 0000000000..ddbf81c930--- /dev/null+++ b/packages/studio/src/components/HighlightedElementSource.tsx@@ -0,0 +1,63 @@+import Prism from 'prismjs';+import React, {useMemo} from 'react';++// These CommonJS grammars read global Prism. Load them after initialization:+// Bun hoists side-effect imports ahead of the CommonJS default import.+Prism.manual = true;+require('prismjs/components/prism-jsx.js');+require('prismjs/components/prism-typescript.js');+require('prismjs/components/prism-tsx.js');++const tokenStyle: React.CSSProperties = {+ fontFamily: 'inherit',+ fontSize: 'inherit',+ lineHeight: 'inherit',+};++const renderTokens = (+ tokens: string | Prism.Token | (string | Prism.Token)[],+): React.ReactNode => {+ if (typeof tokens === 'string') {+ return tokens;+ }++ if (Array.isArray(tokens)) {+ let offset = 0;+ return tokens.map((token) => {+ const key = offset;+ offset += token.length;+ return <React.Fragment key={key}>{renderTokens(token)}</React.Fragment>;+ });+ }++ return (+ <span+ className={[+ 'token',+ tokens.type,+ ...(typeof tokens.alias === 'string'+ ? [tokens.alias]+ : (tokens.alias ?? [])),+ ].join(' ')}+ style={tokenStyle}+ >+ {renderTokens(tokens.content)}+ </span>+ );+};++const HighlightedElementSource: React.FC<{readonly source: string}> = ({+ source,+}) => {+ const tokens = useMemo(+ () => Prism.tokenize(source, Prism.languages.tsx),+ [source],+ );+ return (+ <span style={{...tokenStyle, color: 'inherit'}}>+ {renderTokens(tokens)}+ </span>+ );+};++export default HighlightedElementSource;diff --git a/packages/studio/src/helpers/inject-css.ts b/packages/studio/src/helpers/inject-css.tsindex b8088c6c40..f7f6de4acc 100644--- a/packages/studio/src/helpers/inject-css.ts+++ b/packages/studio/src/helpers/inject-css.ts@@ -9,6 +9,7 @@ import {WHITE,} from './colors';import {FOCUS_VISIBLE_ONLY_CLASS_NAME, makeHoverableCSS} from './hoverable';+import {prismTomorrow} from './prism-tomorrow';const makeDefaultGlobalCSS = () => {const dragAreaFactor = 2;@@ -176,6 +177,7 @@ export const injectCSS = () => {return;}+ Internals.CSSUtils.injectCSS(prismTomorrow);Internals.CSSUtils.injectCSS(makeDefaultGlobalCSS());injected = true;};diff --git a/packages/studio/src/helpers/prism-tomorrow.ts b/packages/studio/src/helpers/prism-tomorrow.tsnew file mode 100644index 0000000000..04d7e2ed30--- /dev/null+++ b/packages/studio/src/helpers/prism-tomorrow.ts@@ -0,0 +1,148 @@+// Unmodified [email protected]/themes/prism-tomorrow.css, stored as a string for+// Studio’s CSS injection (including Browser Studio, which has no CSS loader).+/*+MIT LICENSE++Copyright (c) 2012 Lea Verou++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.+*/+export const prismTomorrow = `/**+ * prism.js tomorrow night eighties for JavaScript, CoffeeScript, CSS and HTML+ * Based on https://github.com/chriskempson/tomorrow-theme+ * @author Rose Pritchard+ */++code[class*="language-"],+pre[class*="language-"] {+ color: #ccc;+ background: none;+ font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;+ font-size: 1em;+ text-align: left;+ white-space: pre;+ word-spacing: normal;+ word-break: normal;+ word-wrap: normal;+ line-height: 1.5;++ -moz-tab-size: 4;+ -o-tab-size: 4;+ tab-size: 4;++ -webkit-hyphens: none;+ -moz-hyphens: none;+ -ms-hyphens: none;+ hyphens: none;++}++/* Code blocks */+pre[class*="language-"] {+ padding: 1em;+ margin: .5em 0;+ overflow: auto;+}++:not(pre) > code[class*="language-"],+pre[class*="language-"] {+ background: #2d2d2d;+}++/* Inline code */+:not(pre) > code[class*="language-"] {+ padding: .1em;+ border-radius: .3em;+ white-space: normal;+}++.token.comment,+.token.block-comment,+.token.prolog,+.token.doctype,+.token.cdata {+ color: #999;+}++.token.punctuation {+ color: #ccc;+}++.token.tag,+.token.attr-name,+.token.namespace,+.token.deleted {+ color: #e2777a;+}++.token.function-name {+ color: #6196cc;+}++.token.boolean,+.token.number,+.token.function {+ color: #f08d49;+}++.token.property,+.token.class-name,+.token.constant,+.token.symbol {+ color: #f8c555;+}++.token.selector,+.token.important,+.token.atrule,+.token.keyword,+.token.builtin {+ color: #cc99cd;+}++.token.string,+.token.char,+.token.attr-value,+.token.regex,+.token.variable {+ color: #7ec699;+}++.token.operator,+.token.entity,+.token.url {+ color: #67cdcc;+}++.token.important,+.token.bold {+ font-weight: bold;+}+.token.italic {+ font-style: italic;+}++.token.entity {+ cursor: help;+}++.token.inserted {+ color: green;+}+`;