refactor: cleanup draft flag which is confusing

This commit is contained in:
Peng Xiao
2022-07-01 11:37:03 +08:00
parent b9647c95a6
commit 931a601dfa
9 changed files with 23 additions and 43 deletions

View File

@@ -182,8 +182,9 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
const isMoving = useCameraMovingRef()
const { Page } = React.useContext(LogseqContext)
const isSelected = app.selectedIds.has(this.id)
const isCreating = app.isIn('logseq-portal.creating') && !pageId
const tlEventsEnabled =
(isMoving || (isSelected && !isEditing) || app.selectedTool.id !== 'select') && !this.draft
(isMoving || (isSelected && !isEditing) || app.selectedTool.id !== 'select') && !isCreating
const stop = React.useCallback(
e => {
if (!tlEventsEnabled) {
@@ -213,16 +214,14 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
}, [isEditing, this.props.collapsed])
const onPageNameChanged = React.useCallback((id: string) => {
app.history.resume()
app.wrapUpdate(() => {
this.setDraft(false)
this.update({
pageId: id,
size: [600, 320],
blockType: 'P',
})
app.selectTool('select')
this.update({
pageId: id,
size: [600, 320],
blockType: 'P',
})
app.selectTool('select')
app.history.resume()
app.history.persist()
}, [])
if (!Page) {
@@ -247,7 +246,7 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
pointerEvents: isEditing ? 'all' : 'none',
}}
>
{this.draft ? (
{isCreating ? (
<LogseqQuickSearch onChange={onPageNameChanged} />
) : (
<div

View File

@@ -26,7 +26,6 @@ export class CreatingState extends TLToolState<
point: Vec.sub(this.app.inputs.originPoint, this.offset),
size: LogseqPortalShape.defaultProps.size,
} as any)
shape.setDraft(true)
this.creatingShape = shape
this.app.currentPage.addShapes(shape)
this.app.setEditingShape(shape)
@@ -57,12 +56,13 @@ export class CreatingState extends TLToolState<
onExit = () => {
if (!this.creatingShape) return
if (this.creatingShape?.draft) {
this.app.deleteShapes([this.creatingShape.id])
} else {
this.app.setSelectedShapes([this.creatingShape.id])
}
this.app.clearEditingShape()
this.app.history.resume()
if (this.creatingShape?.props.pageId) {
this.app.setSelectedShapes([this.creatingShape.id])
} else {
this.app.deleteShapes([this.creatingShape.id])
}
}
}

View File

@@ -12,13 +12,13 @@ export class IdleState extends TLToolState<
static id = 'idle'
cursor = TLCursor.Cross
onEnter = ({ quick }: { quick: boolean } = { quick: false }) => {
onEnter = ({ quick }: { quick?: boolean }) => {
if (quick) {
this.tool.transition('creating')
}
}
onPointerDown: TLReactEvents<Shape>['pointer'] = (e) => {
onPointerDown: TLReactEvents<Shape>['pointer'] = e => {
this.tool.transition('creating')
}
}