Skip to content

Commit

Permalink
feat: add scale option to shapes and arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc authored and tiensonqin committed Dec 28, 2022
1 parent 2010ccf commit 5b1a323
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 16 deletions.
Expand Up @@ -63,10 +63,10 @@ export const shapeMapping: Record<ShapeType, ContextBarActionType[]> = {
],
youtube: ['YoutubeLink', 'Links'],
iframe: ['IFrameSource', 'Links'],
box: ['Edit', 'TextStyle', 'Swatch', 'NoFill', 'StrokeType', 'Links'],
ellipse: ['Edit', 'TextStyle', 'Swatch', 'NoFill', 'StrokeType', 'Links'],
polygon: ['Edit', 'TextStyle', 'Swatch', 'NoFill', 'StrokeType', 'Links'],
line: ['Edit', 'TextStyle', 'Swatch', 'ArrowMode', 'Links'],
box: ['Edit', 'TextStyle', 'Swatch', 'ScaleLevel', 'NoFill', 'StrokeType', 'Links'],
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'],
text: ['Edit', 'TextStyle', 'Swatch', 'ScaleLevel', 'AutoResizing', 'Links'],
Expand Down
33 changes: 31 additions & 2 deletions tldraw/apps/tldraw-logseq/src/lib/shapes/BoxShape.tsx
Expand Up @@ -7,15 +7,29 @@ import { observer } from 'mobx-react-lite'
import { CustomStyleProps, withClampedStyles } from './style-props'
import { BindingIndicator } from './BindingIndicator'
import { TextLabel } from './text/TextLabel'
import type { SizeLevel } from '.'
import { action, computed } from 'mobx'

export interface BoxShapeProps extends TLBoxShapeProps, CustomStyleProps {
borderRadius: number
type: 'box'
label: string
fontSize: number
fontWeight: number
italic: boolean
scaleLevel?: SizeLevel
}

const font = '18px / 1 var(--ls-font-family)'
const font = '20px / 1 var(--ls-font-family)'

const levelToScale = {
xs: 10,
sm: 16,
md: 20,
lg: 32,
xl: 48,
xxl: 60,
}

export class BoxShape extends TLBoxShape<BoxShapeProps> {
static id = 'box'
Expand All @@ -31,6 +45,7 @@ export class BoxShape extends TLBoxShape<BoxShapeProps> {
fill: '',
noFill: false,
fontWeight: 400,
fontSize: 20,
italic: false,
strokeType: 'line',
strokeWidth: 2,
Expand All @@ -55,14 +70,15 @@ export class BoxShape extends TLBoxShape<BoxShapeProps> {
label,
italic,
fontWeight,
fontSize,
},
} = this

const labelSize =
label || isEditing
? getTextLabelSize(
label,
{ fontFamily: 'var(--ls-font-family)', fontSize: 18, lineHeight: 1, fontWeight },
{ fontFamily: 'var(--ls-font-family)', fontSize, lineHeight: 1, fontWeight },
4
)
: [0, 0]
Expand All @@ -89,6 +105,7 @@ export class BoxShape extends TLBoxShape<BoxShapeProps> {
color={getComputedColor(stroke, 'text')}
offsetX={offset[0]}
offsetY={offset[1]}
fontSize={fontSize}
scale={scale}
isEditing={isEditing}
onChange={handleLabelChange}
Expand Down Expand Up @@ -127,6 +144,18 @@ export class BoxShape extends TLBoxShape<BoxShapeProps> {
}
)

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

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

ReactIndicator = observer(() => {
const {
props: {
Expand Down
31 changes: 30 additions & 1 deletion tldraw/apps/tldraw-logseq/src/lib/shapes/EllipseShape.tsx
Expand Up @@ -11,16 +11,30 @@ import * as React from 'react'
import { observer } from 'mobx-react-lite'
import { CustomStyleProps, withClampedStyles } from './style-props'
import { TextLabel } from './text/TextLabel'
import type { SizeLevel } from '.'
import { action, computed } from 'mobx'

export interface EllipseShapeProps extends TLEllipseShapeProps, CustomStyleProps {
type: 'ellipse'
size: number[]
label: string
fontSize: number
fontWeight: number
italic: boolean
scaleLevel?: SizeLevel
}

const font = '18px / 1 var(--ls-font-family)'

const levelToScale = {
xs: 10,
sm: 16,
md: 20,
lg: 32,
xl: 48,
xxl: 60,
}

export class EllipseShape extends TLEllipseShape<EllipseShapeProps> {
static id = 'ellipse'

Expand All @@ -34,6 +48,7 @@ export class EllipseShape extends TLEllipseShape<EllipseShapeProps> {
fill: '',
noFill: false,
fontWeight: 400,
fontSize: 20,
italic: false,
strokeType: 'line',
strokeWidth: 2,
Expand All @@ -56,13 +71,14 @@ export class EllipseShape extends TLEllipseShape<EllipseShapeProps> {
label,
italic,
fontWeight,
fontSize,
} = this.props

const labelSize =
label || isEditing
? getTextLabelSize(
label,
{ fontFamily: 'var(--ls-font-family)', fontSize: 18, lineHeight: 1, fontWeight },
{ fontFamily: 'var(--ls-font-family)', fontSize, lineHeight: 1, fontWeight },
4
)
: [0, 0]
Expand Down Expand Up @@ -94,6 +110,7 @@ export class EllipseShape extends TLEllipseShape<EllipseShapeProps> {
onChange={handleLabelChange}
onBlur={onEditingEnd}
fontStyle={italic ? 'italic' : 'normal'}
fontSize={fontSize}
fontWeight={fontWeight}
pointerEvents={!!label}
/>
Expand Down Expand Up @@ -121,6 +138,18 @@ export class EllipseShape extends TLEllipseShape<EllipseShapeProps> {
}
)

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

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

ReactIndicator = observer(() => {
const {
size: [w, h],
Expand Down
52 changes: 49 additions & 3 deletions tldraw/apps/tldraw-logseq/src/lib/shapes/LineShape.tsx
Expand Up @@ -10,15 +10,28 @@ import { CustomStyleProps, withClampedStyles } from './style-props'
import { getTextLabelSize } from '@tldraw/core'
import { LabelMask } from './text/LabelMask'
import { TextLabel } from './text/TextLabel'
import type { SizeLevel } from '.'
import { action, computed } from 'mobx'

interface LineShapeProps extends CustomStyleProps, TLLineShapeProps {
type: 'line'
label: string
fontSize: number
fontWeight: number
italic: boolean
scaleLevel?: SizeLevel
}

const font = '18px / 1 var(--ls-font-family)'
const font = '20px / 1 var(--ls-font-family)'

const levelToScale = {
xs: 10,
sm: 16,
md: 20,
lg: 32,
xl: 48,
xxl: 60,
}

export class LineShape extends TLLineShape<LineShapeProps> {
static id = 'line'
Expand All @@ -36,6 +49,7 @@ export class LineShape extends TLLineShape<LineShapeProps> {
fill: '',
noFill: true,
fontWeight: 400,
fontSize: 20,
italic: false,
strokeType: 'line',
strokeWidth: 1,
Expand All @@ -57,9 +71,17 @@ export class LineShape extends TLLineShape<LineShapeProps> {
label,
italic,
fontWeight,
fontSize,
id,
} = this.props
const labelSize = label || isEditing ? getTextLabelSize(label, font, 6) : [0, 0]
const labelSize =
label || isEditing
? getTextLabelSize(
label,
{ fontFamily: 'var(--ls-font-family)', fontSize, lineHeight: 1, fontWeight },
6
)
: [0, 0]
const midPoint = Vec.med(start.point, end.point)
const dist = Vec.dist(start.point, end.point)
const scale = Math.max(
Expand All @@ -81,6 +103,7 @@ export class LineShape extends TLLineShape<LineShapeProps> {
<TextLabel
font={font}
text={label}
fontSize={fontSize}
color={getComputedColor(stroke, 'text')}
offsetX={offset[0]}
offsetY={offset[1]}
Expand All @@ -102,16 +125,37 @@ export class LineShape extends TLLineShape<LineShapeProps> {
)
})

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

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

ReactIndicator = observer(({ isEditing }: TLComponentProps) => {
const {
id,
decorations,
label,
strokeWidth,
fontSize,
fontWeight,
handles: { start, end },
} = this.props
const bounds = this.getBounds()
const labelSize = label ? getTextLabelSize(label, font, 6) : [0, 0]
const labelSize =
label || isEditing
? getTextLabelSize(
label,
{ fontFamily: 'var(--ls-font-family)', fontSize, lineHeight: 1, fontWeight },
6
)
: [0, 0]
const midPoint = Vec.med(start.point, end.point)
const dist = Vec.dist(start.point, end.point)
const scale = Math.max(
Expand Down Expand Up @@ -160,6 +204,7 @@ export class LineShape extends TLLineShape<LineShapeProps> {
strokeType,
decorations,
label,
scaleLevel,
handles: { start, end },
} = this.props
const midPoint = Vec.med(start.point, end.point)
Expand All @@ -172,6 +217,7 @@ export class LineShape extends TLLineShape<LineShapeProps> {
strokeWidth,
strokeType,
}}
scaleLevel={scaleLevel}
start={start.point}
end={end.point}
decorationStart={decorations?.start}
Expand Down
32 changes: 30 additions & 2 deletions tldraw/apps/tldraw-logseq/src/lib/shapes/PolygonShape.tsx
Expand Up @@ -11,15 +11,28 @@ import * as React from 'react'
import { observer } from 'mobx-react-lite'
import { CustomStyleProps, withClampedStyles } from './style-props'
import { TextLabel } from './text/TextLabel'
import type { SizeLevel } from '.'
import { action, computed } from 'mobx'

interface PolygonShapeProps extends TLPolygonShapeProps, CustomStyleProps {
type: 'polygon'
label: string
fontSize: number
fontWeight: number
italic: boolean
scaleLevel?: SizeLevel
}

const font = '18px / 1 var(--ls-font-family)'
const font = '20px / 1 var(--ls-font-family)'

const levelToScale = {
xs: 10,
sm: 16,
md: 20,
lg: 32,
xl: 48,
xxl: 60,
}

export class PolygonShape extends TLPolygonShape<PolygonShapeProps> {
static id = 'polygon'
Expand All @@ -36,6 +49,7 @@ export class PolygonShape extends TLPolygonShape<PolygonShapeProps> {
stroke: '',
fill: '',
fontWeight: 400,
fontSize: 20,
italic: false,
noFill: false,
strokeType: 'line',
Expand All @@ -60,6 +74,7 @@ export class PolygonShape extends TLPolygonShape<PolygonShapeProps> {
label,
italic,
fontWeight,
fontSize,
},
} = this

Expand All @@ -69,7 +84,7 @@ export class PolygonShape extends TLPolygonShape<PolygonShapeProps> {
label || isEditing
? getTextLabelSize(
label,
{ fontFamily: 'var(--ls-font-family)', fontSize: 18, lineHeight: 1, fontWeight },
{ fontFamily: 'var(--ls-font-family)', fontSize, lineHeight: 1, fontWeight },
4
)
: [0, 0]
Expand Down Expand Up @@ -102,6 +117,7 @@ export class PolygonShape extends TLPolygonShape<PolygonShapeProps> {
<TextLabel
font={font}
text={label}
fontSize={fontSize}
color={getComputedColor(stroke, 'text')}
offsetX={offset[0]}
offsetY={offset[1] / scale}
Expand Down Expand Up @@ -136,6 +152,18 @@ export class PolygonShape extends TLPolygonShape<PolygonShapeProps> {
}
)

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

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

ReactIndicator = observer(() => {
const {
offset: [x, y],
Expand Down

0 comments on commit 5b1a323

Please sign in to comment.