Skip to content

Commit

Permalink
feat: add scale option to pencil and highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc authored and tiensonqin committed Jan 5, 2023
1 parent c33fc8a commit 21ddb7e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Expand Up @@ -67,8 +67,8 @@ export const shapeMapping: Record<ShapeType, ContextBarActionType[]> = {
ellipse: ['Edit', 'TextStyle', 'Swatch', 'ScaleLevel', 'NoFill', 'StrokeType', 'Links'],
polygon: ['Edit', 'TextStyle', 'Swatch', 'ScaleLevel', 'NoFill', 'StrokeType', 'Links'],
line: ['Edit', 'TextStyle', 'Swatch', 'ScaleLevel', 'ArrowMode', 'Links'],
pencil: ['Swatch', 'Links'],
highlighter: ['Swatch', 'Links'],
pencil: ['Swatch', 'Links', 'ScaleLevel'],
highlighter: ['Swatch', 'Links', 'ScaleLevel'],
text: ['Edit', 'TextStyle', 'Swatch', 'ScaleLevel', 'AutoResizing', 'Links'],
html: ['ScaleLevel', 'AutoResizing', 'Links'],
image: ['Links'],
Expand Down
25 changes: 24 additions & 1 deletion tldraw/apps/tldraw-logseq/src/lib/shapes/HighlighterShape.tsx
Expand Up @@ -2,11 +2,22 @@
import { SvgPathUtils, TLDrawShape, TLDrawShapeProps, getComputedColor } from '@tldraw/core'
import { SVGContainer, TLComponentProps } from '@tldraw/react'
import { observer } from 'mobx-react-lite'
import { computed, makeObservable } from 'mobx'
import { action, computed, makeObservable } from 'mobx'
import type { SizeLevel } from '.'
import { CustomStyleProps, withClampedStyles } from './style-props'

export interface HighlighterShapeProps extends TLDrawShapeProps, CustomStyleProps {
type: 'highlighter'
scaleLevel?: SizeLevel
}

const levelToScale = {
xs: 1,
sm: 1.6,
md: 2,
lg: 3.2,
xl: 4.8,
xxl: 6,
}

export class HighlighterShape extends TLDrawShape<HighlighterShapeProps> {
Expand Down Expand Up @@ -59,6 +70,18 @@ export class HighlighterShape extends TLDrawShape<HighlighterShapeProps> {
)
})

@computed get scaleLevel() {
return this.props.scaleLevel ?? 'md'
}

@action setScaleLevel = async (v?: SizeLevel) => {
this.update({
scaleLevel: v,
strokeWidth: levelToScale[v ?? 'md'],
})
this.onResetBounds()
}

ReactIndicator = observer(() => {
const { pointsPath } = this
return <path d={pointsPath} fill="none" />
Expand Down
26 changes: 25 additions & 1 deletion tldraw/apps/tldraw-logseq/src/lib/shapes/PencilShape.tsx
Expand Up @@ -2,18 +2,29 @@
import { SvgPathUtils, TLDrawShape, TLDrawShapeProps, getComputedColor } from '@tldraw/core'
import { SVGContainer, TLComponentProps } from '@tldraw/react'
import Vec from '@tldraw/vec'
import { computed, makeObservable } from 'mobx'
import { action, computed, makeObservable } from 'mobx'
import { observer } from 'mobx-react-lite'
import {
getStrokeOutlinePoints,
getStrokePoints,
StrokeOptions,
StrokePoint,
} from 'perfect-freehand'
import type { SizeLevel } from '.'
import { CustomStyleProps, withClampedStyles } from './style-props'

export interface PencilShapeProps extends TLDrawShapeProps, CustomStyleProps {
type: 'pencil'
scaleLevel?: SizeLevel
}

const levelToScale = {
xs: 1,
sm: 1.6,
md: 2,
lg: 3.2,
xl: 4.8,
xxl: 6,
}

const simulatePressureSettings: StrokeOptions = {
Expand Down Expand Up @@ -108,6 +119,19 @@ export class PencilShape extends TLDrawShape<PencilShapeProps> {
)
})


@computed get scaleLevel() {
return this.props.scaleLevel ?? 'md'
}

@action setScaleLevel = async (v?: SizeLevel) => {
this.update({
scaleLevel: v,
strokeWidth: levelToScale[v ?? 'md'],
})
this.onResetBounds()
}

ReactIndicator = observer(() => {
const { pointsPath } = this
return <path d={pointsPath} />
Expand Down

0 comments on commit 21ddb7e

Please sign in to comment.