Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monaco editor and show it on click. #21

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
63414b7
Add monaco editor and show it on click.
MitjaBezensek Nov 20, 2023
4c325d3
Merge branch 'main' into mitja/add-code-editor
MitjaBezensek Nov 21, 2023
88e81e8
Refactor.
MitjaBezensek Nov 21, 2023
3856c72
Fix merge conflict.
MitjaBezensek Nov 21, 2023
ff3c6e0
Remove stray paren.
MitjaBezensek Nov 21, 2023
2cab2ba
Only add the hack once.
MitjaBezensek Nov 21, 2023
bd99024
Add the editor below.
MitjaBezensek Nov 21, 2023
8cee627
Show the editor to the right.
MitjaBezensek Nov 21, 2023
ce253f6
Add editor infront of canvas.
MitjaBezensek Nov 22, 2023
1f845d9
Merge branch 'main' into mitja/add-code-editor
MitjaBezensek Nov 22, 2023
f8515f1
Fix imports.
MitjaBezensek Nov 22, 2023
d1b0ded
Update preview shape.
MitjaBezensek Nov 22, 2023
c60efa2
Fix buttons.
MitjaBezensek Nov 22, 2023
340dda3
Remove unused variable.
MitjaBezensek Nov 22, 2023
5bfe4af
Remove unused imports.
MitjaBezensek Nov 22, 2023
762cb5b
Send updates to verce.
MitjaBezensek Nov 22, 2023
3b37765
Improve button, remove consoles.
MitjaBezensek Nov 22, 2023
6dc4c15
Show saving indicator. Remove scrollbar.
MitjaBezensek Nov 22, 2023
5c4a539
Update appearance.
MitjaBezensek Nov 22, 2023
1baabce
Remove unused.
MitjaBezensek Nov 22, 2023
1928745
Move the camera.
MitjaBezensek Nov 23, 2023
9dccab6
Refactor.
MitjaBezensek Nov 23, 2023
9383dda
Fix selections.
MitjaBezensek Nov 23, 2023
d8f0b72
Dont show current line selection.
MitjaBezensek Nov 23, 2023
d876337
Remove unused import.
MitjaBezensek Nov 23, 2023
e23cbce
Show different icon when showing editor.
MitjaBezensek Nov 23, 2023
360ff4d
Refactor.
MitjaBezensek Nov 23, 2023
3b48024
Remove unused import.
MitjaBezensek Nov 23, 2023
81b6f65
Imporve ux.
MitjaBezensek Nov 23, 2023
7129f47
Add focus mode.
MitjaBezensek Nov 24, 2023
2f7a84b
Remove unused variable.
MitjaBezensek Nov 24, 2023
2000bc7
Remove console.log.
MitjaBezensek Nov 27, 2023
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
Prev Previous commit
Next Next commit
Merge branch 'main' into mitja/add-code-editor
MitjaBezensek committed Nov 21, 2023
commit 4c325d38866be6109203c08b23d565928a82f23a
164 changes: 39 additions & 125 deletions app/PreviewShape/PreviewShape.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { Editor as MonacoEditor, OnChange } from '@monaco-editor/react'
import {
BaseBoxShapeUtil,
DefaultSpinner,
Editor,
HTMLContainer,
Icon,
TLBaseShape,
Vec2d,
stopEventPropagation,
toDomPrecision,
useIsDarkMode,
useIsEditing,
useToasts,
useValue,
} from '@tldraw/tldraw'
import { useCallback, useState } from 'react'
import { useState } from 'react'
import { ShowEditorButton } from '../components/ShowEditorButton'
import { ShowResult } from '../components/ShowResult'
import { UrlLinkButton } from '../components/UrlLinkButton'

export type PreviewShape = TLBaseShape<
'preview',
@@ -26,74 +26,6 @@ export type PreviewShape = TLBaseShape<
}
>

export function ShowResult({
boxShadow,
editor,
html,
isEditing,
isShowingEditor,
shape,
}: {
boxShadow: string
editor: Editor
html: string
isEditing: boolean
isShowingEditor: boolean
shape: PreviewShape
}) {
const dark = useIsDarkMode()

const handleOnChange: OnChange = useCallback(
(value, _event) => {
editor.updateShape({
id: shape.id,
type: shape.type,
props: {
html: value,
},
})
},
[editor, shape.id, shape.type]
)

return (
<>
{isShowingEditor && (
<div style={{ width: '2000px', height: '100%' }}>
<MonacoEditor
defaultLanguage="html"
defaultValue={html}
onChange={handleOnChange}
theme={dark ? 'vs-dark' : 'vs-light'}
options={{
minimap: {
enabled: false,
},
lineNumbers: 'off',
wordWrap: 'wordWrapColumn',
wordWrapColumn: 80,
fontSize: 13,
}}
/>
</div>
)}
<iframe
srcDoc={html}
width={toDomPrecision(shape.props.w)}
height={toDomPrecision(shape.props.h)}
draggable={false}
style={{
pointerEvents: isEditing ? 'auto' : 'none',
boxShadow,
border: '1px solid var(--color-panel-contrast)',
borderRadius: 'var(--radius-2)',
}}
/>
)
</>
)
}

export class PreviewShapeUtil extends BaseBoxShapeUtil<PreviewShape> {
static override type = 'preview' as const

@@ -166,60 +98,42 @@ export class PreviewShapeUtil extends BaseBoxShapeUtil<PreviewShape> {
</div>
)}
{htmlToUse && (
<>
<button
style={{
all: 'unset',
position: 'absolute',
top: 0,
right: -40,
height: 40,
width: 40,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'pointer',
pointerEvents: 'all',
}}
onClick={() => {
if (navigator && navigator.clipboard) {
navigator.clipboard.writeText(shape.props.html)
toast.addToast({
icon: 'code',
title: 'Copied to clipboard',
})
}
}}
onPointerDown={stopEventPropagation}
title="Copy code to clipboard"
>
<Icon icon="code" />
</button>
<button
style={{
all: 'unset',
position: 'absolute',
top: 30,
right: -40,
height: 40,
width: 40,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'pointer',
pointerEvents: 'all',
}}
onClick={() => {
setIsShowingEditor(!isShowingEditor)
}}
onPointerDown={stopEventPropagation}
title="Show code"
>
<Icon icon="follow" />
</button>
</>
<button
style={{
all: 'unset',
position: 'absolute',
top: 0,
right: -40,
height: 40,
width: 40,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'pointer',
pointerEvents: 'all',
}}
onClick={() => {
if (navigator && navigator.clipboard) {
navigator.clipboard.writeText(shape.props.html)
toast.addToast({
icon: 'code',
title: 'Copied html to clipboard',
})
}
}}
onPointerDown={stopEventPropagation}
title="Copy code to clipboard"
>
<Icon icon="code" />
</button>
)}
{htmlToUse && <UrlLinkButton shape={shape} />}
{htmlToUse && (
<ShowEditorButton
isShowingEditor={isShowingEditor}
setIsShowingEditor={setIsShowingEditor}
/>
)}
{htmlToUse && (
<div
style={{
34 changes: 34 additions & 0 deletions app/components/ShowEditorButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Icon, stopEventPropagation } from '@tldraw/tldraw'

export function ShowEditorButton({
isShowingEditor,
setIsShowingEditor,
}: {
isShowingEditor: boolean
setIsShowingEditor: (isShowingEditor: boolean) => void
}) {
return (
<button
style={{
all: 'unset',
position: 'absolute',
top: 30,
right: -40,
height: 40,
width: 40,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'pointer',
pointerEvents: 'all',
}}
onClick={() => {
setIsShowingEditor(!isShowingEditor)
}}
onPointerDown={stopEventPropagation}
title="Show code"
>
<Icon icon="follow" />
</button>
)
}
72 changes: 72 additions & 0 deletions app/components/ShowResult.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Editor as MonacoEditor, OnChange } from '@monaco-editor/react'
import { Editor, toDomPrecision, useIsDarkMode } from '@tldraw/tldraw'
import { useCallback } from 'react'
import { PreviewShape } from '../PreviewShape/PreviewShape'

export function ShowResult({
boxShadow,
editor,
html,
isEditing,
isShowingEditor,
shape,
}: {
boxShadow: string
editor: Editor
html: string
isEditing: boolean
isShowingEditor: boolean
shape: PreviewShape
}) {
const dark = useIsDarkMode()

const handleOnChange: OnChange = useCallback(
(value, _event) => {
editor.updateShape({
id: shape.id,
type: shape.type,
props: {
html: value,
},
})
},
[editor, shape.id, shape.type]
)

return (
<>
{isShowingEditor && (
<div style={{ width: '2000px', height: '100%' }}>
<MonacoEditor
defaultLanguage="html"
defaultValue={html}
onChange={handleOnChange}
theme={dark ? 'vs-dark' : 'vs-light'}
options={{
minimap: {
enabled: false,
},
lineNumbers: 'off',
wordWrap: 'wordWrapColumn',
wordWrapColumn: 80,
fontSize: 13,
}}
/>
</div>
)}
<iframe
srcDoc={html}
width={toDomPrecision(shape.props.w)}
height={toDomPrecision(shape.props.h)}
draggable={false}
style={{
pointerEvents: isEditing ? 'auto' : 'none',
boxShadow,
border: '1px solid var(--color-panel-contrast)',
borderRadius: 'var(--radius-2)',
}}
/>
)
</>
)
}
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

You are viewing a condensed version of this merge commit. You can view the full changes here.