2024-03-01 18:16:27 +00:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
tldraw zero - package shuffle (#1710)
This PR moves code between our packages so that:
- @tldraw/editor is a “core” library with the engine and canvas but no
shapes, tools, or other things
- @tldraw/tldraw contains everything particular to the experience we’ve
built for tldraw
At first look, this might seem like a step away from customization and
configuration, however I believe it greatly increases the configuration
potential of the @tldraw/editor while also providing a more accurate
reflection of what configuration options actually exist for
@tldraw/tldraw.
## Library changes
@tldraw/editor re-exports its dependencies and @tldraw/tldraw re-exports
@tldraw/editor.
- users of @tldraw/editor WITHOUT @tldraw/tldraw should almost always
only import things from @tldraw/editor.
- users of @tldraw/tldraw should almost always only import things from
@tldraw/tldraw.
- @tldraw/polyfills is merged into @tldraw/editor
- @tldraw/indices is merged into @tldraw/editor
- @tldraw/primitives is merged mostly into @tldraw/editor, partially
into @tldraw/tldraw
- @tldraw/file-format is merged into @tldraw/tldraw
- @tldraw/ui is merged into @tldraw/tldraw
Many (many) utils and other code is moved from the editor to tldraw. For
example, embeds now are entirely an feature of @tldraw/tldraw. The only
big chunk of code left in core is related to arrow handling.
## API Changes
The editor can now be used without tldraw's assets. We load them in
@tldraw/tldraw instead, so feel free to use whatever fonts or images or
whatever that you like with the editor.
All tools and shapes (except for the `Group` shape) are moved to
@tldraw/tldraw. This includes the `select` tool.
You should use the editor with at least one tool, however, so you now
also need to send in an `initialState` prop to the Editor /
<TldrawEditor> component indicating which state the editor should begin
in.
The `components` prop now also accepts `SelectionForeground`.
The complex selection component that we use for tldraw is moved to
@tldraw/tldraw. The default component is quite basic but can easily be
replaced via the `components` prop. We pass down our tldraw-flavored
SelectionFg via `components`.
Likewise with the `Scribble` component: the `DefaultScribble` no longer
uses our freehand tech and is a simple path instead. We pass down the
tldraw-flavored scribble via `components`.
The `ExternalContentManager` (`Editor.externalContentManager`) is
removed and replaced with a mapping of types to handlers.
- Register new content handlers with
`Editor.registerExternalContentHandler`.
- Register new asset creation handlers (for files and URLs) with
`Editor.registerExternalAssetHandler`
### Change Type
- [x] `major` — Breaking change
### Test Plan
- [x] Unit Tests
- [x] End to end tests
### Release Notes
- [@tldraw/editor] lots, wip
- [@tldraw/ui] gone, merged to tldraw/tldraw
- [@tldraw/polyfills] gone, merged to tldraw/editor
- [@tldraw/primitives] gone, merged to tldraw/editor / tldraw/tldraw
- [@tldraw/indices] gone, merged to tldraw/editor
- [@tldraw/file-format] gone, merged to tldraw/tldraw
---------
Co-authored-by: alex <alex@dytry.ch>
2023-07-17 21:22:34 +00:00
|
|
|
import {
|
|
|
|
BaseBoxShapeUtil,
|
|
|
|
HTMLContainer,
|
|
|
|
TLVideoShape,
|
|
|
|
toDomPrecision,
|
|
|
|
useIsEditing,
|
|
|
|
videoShapeMigrations,
|
|
|
|
videoShapeProps,
|
|
|
|
} from '@tldraw/editor'
|
2024-03-01 18:16:27 +00:00
|
|
|
import { ReactEventHandler, useCallback, useEffect, useRef, useState } from 'react'
|
|
|
|
import { BrokenAssetIcon } from '../shared/BrokenAssetIcon'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { HyperlinkButton } from '../shared/HyperlinkButton'
|
tldraw zero - package shuffle (#1710)
This PR moves code between our packages so that:
- @tldraw/editor is a “core” library with the engine and canvas but no
shapes, tools, or other things
- @tldraw/tldraw contains everything particular to the experience we’ve
built for tldraw
At first look, this might seem like a step away from customization and
configuration, however I believe it greatly increases the configuration
potential of the @tldraw/editor while also providing a more accurate
reflection of what configuration options actually exist for
@tldraw/tldraw.
## Library changes
@tldraw/editor re-exports its dependencies and @tldraw/tldraw re-exports
@tldraw/editor.
- users of @tldraw/editor WITHOUT @tldraw/tldraw should almost always
only import things from @tldraw/editor.
- users of @tldraw/tldraw should almost always only import things from
@tldraw/tldraw.
- @tldraw/polyfills is merged into @tldraw/editor
- @tldraw/indices is merged into @tldraw/editor
- @tldraw/primitives is merged mostly into @tldraw/editor, partially
into @tldraw/tldraw
- @tldraw/file-format is merged into @tldraw/tldraw
- @tldraw/ui is merged into @tldraw/tldraw
Many (many) utils and other code is moved from the editor to tldraw. For
example, embeds now are entirely an feature of @tldraw/tldraw. The only
big chunk of code left in core is related to arrow handling.
## API Changes
The editor can now be used without tldraw's assets. We load them in
@tldraw/tldraw instead, so feel free to use whatever fonts or images or
whatever that you like with the editor.
All tools and shapes (except for the `Group` shape) are moved to
@tldraw/tldraw. This includes the `select` tool.
You should use the editor with at least one tool, however, so you now
also need to send in an `initialState` prop to the Editor /
<TldrawEditor> component indicating which state the editor should begin
in.
The `components` prop now also accepts `SelectionForeground`.
The complex selection component that we use for tldraw is moved to
@tldraw/tldraw. The default component is quite basic but can easily be
replaced via the `components` prop. We pass down our tldraw-flavored
SelectionFg via `components`.
Likewise with the `Scribble` component: the `DefaultScribble` no longer
uses our freehand tech and is a simple path instead. We pass down the
tldraw-flavored scribble via `components`.
The `ExternalContentManager` (`Editor.externalContentManager`) is
removed and replaced with a mapping of types to handlers.
- Register new content handlers with
`Editor.registerExternalContentHandler`.
- Register new asset creation handlers (for files and URLs) with
`Editor.registerExternalAssetHandler`
### Change Type
- [x] `major` — Breaking change
### Test Plan
- [x] Unit Tests
- [x] End to end tests
### Release Notes
- [@tldraw/editor] lots, wip
- [@tldraw/ui] gone, merged to tldraw/tldraw
- [@tldraw/polyfills] gone, merged to tldraw/editor
- [@tldraw/primitives] gone, merged to tldraw/editor / tldraw/tldraw
- [@tldraw/indices] gone, merged to tldraw/editor
- [@tldraw/file-format] gone, merged to tldraw/tldraw
---------
Co-authored-by: alex <alex@dytry.ch>
2023-07-17 21:22:34 +00:00
|
|
|
import { usePrefersReducedMotion } from '../shared/usePrefersReducedMotion'
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
/** @public */
|
2023-06-04 10:38:53 +00:00
|
|
|
export class VideoShapeUtil extends BaseBoxShapeUtil<TLVideoShape> {
|
2023-06-12 14:04:14 +00:00
|
|
|
static override type = 'video' as const
|
tldraw zero - package shuffle (#1710)
This PR moves code between our packages so that:
- @tldraw/editor is a “core” library with the engine and canvas but no
shapes, tools, or other things
- @tldraw/tldraw contains everything particular to the experience we’ve
built for tldraw
At first look, this might seem like a step away from customization and
configuration, however I believe it greatly increases the configuration
potential of the @tldraw/editor while also providing a more accurate
reflection of what configuration options actually exist for
@tldraw/tldraw.
## Library changes
@tldraw/editor re-exports its dependencies and @tldraw/tldraw re-exports
@tldraw/editor.
- users of @tldraw/editor WITHOUT @tldraw/tldraw should almost always
only import things from @tldraw/editor.
- users of @tldraw/tldraw should almost always only import things from
@tldraw/tldraw.
- @tldraw/polyfills is merged into @tldraw/editor
- @tldraw/indices is merged into @tldraw/editor
- @tldraw/primitives is merged mostly into @tldraw/editor, partially
into @tldraw/tldraw
- @tldraw/file-format is merged into @tldraw/tldraw
- @tldraw/ui is merged into @tldraw/tldraw
Many (many) utils and other code is moved from the editor to tldraw. For
example, embeds now are entirely an feature of @tldraw/tldraw. The only
big chunk of code left in core is related to arrow handling.
## API Changes
The editor can now be used without tldraw's assets. We load them in
@tldraw/tldraw instead, so feel free to use whatever fonts or images or
whatever that you like with the editor.
All tools and shapes (except for the `Group` shape) are moved to
@tldraw/tldraw. This includes the `select` tool.
You should use the editor with at least one tool, however, so you now
also need to send in an `initialState` prop to the Editor /
<TldrawEditor> component indicating which state the editor should begin
in.
The `components` prop now also accepts `SelectionForeground`.
The complex selection component that we use for tldraw is moved to
@tldraw/tldraw. The default component is quite basic but can easily be
replaced via the `components` prop. We pass down our tldraw-flavored
SelectionFg via `components`.
Likewise with the `Scribble` component: the `DefaultScribble` no longer
uses our freehand tech and is a simple path instead. We pass down the
tldraw-flavored scribble via `components`.
The `ExternalContentManager` (`Editor.externalContentManager`) is
removed and replaced with a mapping of types to handlers.
- Register new content handlers with
`Editor.registerExternalContentHandler`.
- Register new asset creation handlers (for files and URLs) with
`Editor.registerExternalAssetHandler`
### Change Type
- [x] `major` — Breaking change
### Test Plan
- [x] Unit Tests
- [x] End to end tests
### Release Notes
- [@tldraw/editor] lots, wip
- [@tldraw/ui] gone, merged to tldraw/tldraw
- [@tldraw/polyfills] gone, merged to tldraw/editor
- [@tldraw/primitives] gone, merged to tldraw/editor / tldraw/tldraw
- [@tldraw/indices] gone, merged to tldraw/editor
- [@tldraw/file-format] gone, merged to tldraw/tldraw
---------
Co-authored-by: alex <alex@dytry.ch>
2023-07-17 21:22:34 +00:00
|
|
|
static override props = videoShapeProps
|
|
|
|
static override migrations = videoShapeMigrations
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
override canEdit = () => true
|
|
|
|
override isAspectRatioLocked = () => true
|
|
|
|
|
`ShapeUtil` refactor, `Editor` cleanup (#1611)
This PR improves the ergonomics of `ShapeUtil` classes.
### Cached methods
First, I've remove the cached methods (such as `bounds`) from the
`ShapeUtil` class and lifted this to the `Editor` class.
Previously, calling `ShapeUtil.getBounds` would return the un-cached
bounds of a shape, while calling `ShapeUtil.bounds` would return the
cached bounds of a shape. We also had `Editor.getBounds`, which would
call `ShapeUtil.bounds`. It was confusing. The cached methods like
`outline` were also marked with "please don't override", which suggested
the architecture was just wrong.
The only weirdness from this is that utils sometimes reach out to the
editor for cached versions of data rather than calling their own cached
methods. It's still an easier story to tell than what we had before.
### More defaults
We now have three and only three `abstract` methods for a `ShapeUtil`:
- `getDefaultProps` (renamed from `defaultProps`)
- `getBounds`,
- `component`
- `indicator`
Previously, we also had `getCenter` as an abstract method, though this
was usually just the middle of the bounds anyway.
### Editing bounds
This PR removes the concept of editingBounds. The viewport will no
longer animate to editing shapes.
### Active area manager
This PR also removes the active area manager, which was not being used
in the way we expected it to be.
### Dpr manager
This PR removes the dpr manager and uses a hook instead to update it
from React. This is one less runtime browser dependency in the app, one
less thing to document.
### Moving things around
This PR also continues to try to organize related methods and properties
in the editor.
### Change Type
- [x] `major` — Breaking change
### Release Notes
- [editor] renames `defaultProps` to `getDefaultProps`
- [editor] removes `outline`, `outlineSegments`, `handles`, `bounds`
- [editor] renames `renderBackground` to `backgroundComponent`
2023-06-19 14:01:18 +00:00
|
|
|
override getDefaultProps(): TLVideoShape['props'] {
|
2023-04-25 11:01:25 +00:00
|
|
|
return {
|
|
|
|
w: 100,
|
|
|
|
h: 100,
|
|
|
|
assetId: null,
|
|
|
|
time: 0,
|
|
|
|
playing: true,
|
|
|
|
url: '',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 09:46:53 +00:00
|
|
|
component(shape: TLVideoShape) {
|
2024-03-01 18:16:27 +00:00
|
|
|
const { editor } = this
|
|
|
|
const showControls = editor.getShapeGeometry(shape).bounds.w * editor.getZoomLevel() >= 110
|
|
|
|
const asset = shape.props.assetId ? editor.getAsset(shape.props.assetId) : null
|
|
|
|
const { time, playing } = shape.props
|
|
|
|
const isEditing = useIsEditing(shape.id)
|
|
|
|
const prefersReducedMotion = usePrefersReducedMotion()
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
const rVideo = useRef<HTMLVideoElement>(null!)
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
const handlePlay = useCallback<ReactEventHandler<HTMLVideoElement>>(
|
|
|
|
(e) => {
|
|
|
|
const video = e.currentTarget
|
2024-03-02 19:38:21 +00:00
|
|
|
if (!video) return
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
editor.updateShapes([
|
|
|
|
{
|
|
|
|
type: 'video',
|
|
|
|
id: shape.id,
|
|
|
|
props: {
|
|
|
|
playing: true,
|
|
|
|
time: video.currentTime,
|
|
|
|
},
|
2023-04-25 11:01:25 +00:00
|
|
|
},
|
2024-03-01 18:16:27 +00:00
|
|
|
])
|
|
|
|
},
|
|
|
|
[shape.id, editor]
|
|
|
|
)
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
const handlePause = useCallback<ReactEventHandler<HTMLVideoElement>>(
|
|
|
|
(e) => {
|
|
|
|
const video = e.currentTarget
|
2024-03-02 19:38:21 +00:00
|
|
|
if (!video) return
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
editor.updateShapes([
|
2023-04-25 11:01:25 +00:00
|
|
|
{
|
|
|
|
type: 'video',
|
|
|
|
id: shape.id,
|
|
|
|
props: {
|
2024-03-01 18:16:27 +00:00
|
|
|
playing: false,
|
2023-04-25 11:01:25 +00:00
|
|
|
time: video.currentTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
2024-03-01 18:16:27 +00:00
|
|
|
},
|
|
|
|
[shape.id, editor]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleSetCurrentTime = useCallback<ReactEventHandler<HTMLVideoElement>>(
|
|
|
|
(e) => {
|
|
|
|
const video = e.currentTarget
|
2024-03-02 19:38:21 +00:00
|
|
|
if (!video) return
|
2024-03-01 18:16:27 +00:00
|
|
|
|
|
|
|
if (isEditing) {
|
|
|
|
editor.updateShapes([
|
|
|
|
{
|
|
|
|
type: 'video',
|
|
|
|
id: shape.id,
|
|
|
|
props: {
|
|
|
|
time: video.currentTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[isEditing, shape.id, editor]
|
|
|
|
)
|
|
|
|
|
|
|
|
const [isLoaded, setIsLoaded] = useState(false)
|
|
|
|
|
|
|
|
const handleLoadedData = useCallback<ReactEventHandler<HTMLVideoElement>>(
|
|
|
|
(e) => {
|
|
|
|
const video = e.currentTarget
|
2024-03-02 19:38:21 +00:00
|
|
|
if (!video) return
|
2024-03-01 18:16:27 +00:00
|
|
|
if (time !== video.currentTime) {
|
|
|
|
video.currentTime = time
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!playing) {
|
|
|
|
video.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsLoaded(true)
|
|
|
|
},
|
|
|
|
[playing, time]
|
|
|
|
)
|
|
|
|
|
|
|
|
// If the current time changes and we're not editing the video, update the video time
|
|
|
|
useEffect(() => {
|
|
|
|
const video = rVideo.current
|
|
|
|
if (!video) return
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
if (isLoaded && !isEditing && time !== video.currentTime) {
|
2023-04-25 11:01:25 +00:00
|
|
|
video.currentTime = time
|
|
|
|
}
|
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
if (isEditing) {
|
|
|
|
if (document.activeElement !== video) {
|
|
|
|
video.focus()
|
|
|
|
}
|
2023-04-25 11:01:25 +00:00
|
|
|
}
|
2024-03-01 18:16:27 +00:00
|
|
|
}, [isEditing, isLoaded, time])
|
2023-04-25 11:01:25 +00:00
|
|
|
|
2024-03-01 18:16:27 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (prefersReducedMotion) {
|
|
|
|
const video = rVideo.current
|
2024-03-02 19:38:21 +00:00
|
|
|
if (!video) return
|
2024-03-01 18:16:27 +00:00
|
|
|
video.pause()
|
|
|
|
video.currentTime = 0
|
2023-08-25 05:49:06 +00:00
|
|
|
}
|
2024-03-01 18:16:27 +00:00
|
|
|
}, [rVideo, prefersReducedMotion])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<HTMLContainer
|
|
|
|
id={shape.id}
|
|
|
|
style={{
|
|
|
|
color: 'var(--color-text-3)',
|
|
|
|
backgroundColor: asset ? 'transparent' : 'var(--color-low)',
|
|
|
|
border: asset ? 'none' : '1px solid var(--color-low-border)',
|
|
|
|
}}
|
|
|
|
>
|
2024-03-04 16:21:41 +00:00
|
|
|
<div className="tl-counter-scaled">
|
|
|
|
<div className="tl-video-container">
|
|
|
|
{asset?.props.src ? (
|
|
|
|
<video
|
|
|
|
ref={rVideo}
|
|
|
|
style={isEditing ? { pointerEvents: 'all' } : undefined}
|
|
|
|
className={`tl-video tl-video-shape-${shape.id.split(':')[1]}`}
|
|
|
|
width="100%"
|
|
|
|
height="100%"
|
|
|
|
draggable={false}
|
|
|
|
playsInline
|
|
|
|
autoPlay
|
|
|
|
muted
|
|
|
|
loop
|
|
|
|
disableRemotePlayback
|
|
|
|
disablePictureInPicture
|
|
|
|
controls={isEditing && showControls}
|
|
|
|
onPlay={handlePlay}
|
|
|
|
onPause={handlePause}
|
|
|
|
onTimeUpdate={handleSetCurrentTime}
|
|
|
|
onLoadedData={handleLoadedData}
|
|
|
|
hidden={!isLoaded}
|
|
|
|
>
|
|
|
|
<source src={asset.props.src} />
|
|
|
|
</video>
|
|
|
|
) : (
|
|
|
|
<BrokenAssetIcon />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-01 18:16:27 +00:00
|
|
|
</HTMLContainer>
|
|
|
|
{'url' in shape.props && shape.props.url && (
|
|
|
|
<HyperlinkButton url={shape.props.url} zoomLevel={editor.getZoomLevel()} />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
indicator(shape: TLVideoShape) {
|
|
|
|
return <rect width={toDomPrecision(shape.props.w)} height={toDomPrecision(shape.props.h)} />
|
|
|
|
}
|
|
|
|
|
|
|
|
override toSvg(shape: TLVideoShape) {
|
|
|
|
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
|
|
|
|
const image = document.createElementNS('http://www.w3.org/2000/svg', 'image')
|
|
|
|
image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', serializeVideo(shape.id))
|
|
|
|
image.setAttribute('width', shape.props.w.toString())
|
|
|
|
image.setAttribute('height', shape.props.h.toString())
|
|
|
|
g.appendChild(image)
|
|
|
|
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function from v1, could be improved but explicitly using this.model.time (?)
|
|
|
|
function serializeVideo(id: string): string {
|
|
|
|
const splitId = id.split(':')[1]
|
|
|
|
const video = document.querySelector(`.tl-video-shape-${splitId}`) as HTMLVideoElement
|
|
|
|
if (video) {
|
|
|
|
const canvas = document.createElement('canvas')
|
|
|
|
canvas.width = video.videoWidth
|
|
|
|
canvas.height = video.videoHeight
|
|
|
|
canvas.getContext('2d')!.drawImage(video, 0, 0)
|
|
|
|
return canvas.toDataURL('image/png')
|
|
|
|
} else throw new Error('Video with not found when attempting serialization.')
|
|
|
|
}
|