-
-
Notifications
You must be signed in to change notification settings - Fork 415
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
feat(edgeless): auto-connect indicator and index label #5136
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
be15fed
feat(edgeless): auto-connect
regischen 4f0009f
style: adjust line
regischen 829cc4b
test: fix
regischen 89957b2
test: fix
regischen 11dd834
Merge remote-tracking branch 'origin/master' into feat/edgeless-auto-…
regischen 11f4ed7
fix: test
regischen dde4cf4
Merge branch 'master' into feat/edgeless-auto-connect
regischen 19e1e32
test: add
regischen 16c8741
Merge branch 'master' into feat/edgeless-auto-connect
regischen 5448337
style: naming
doodlewind 1c78466
style: naming
doodlewind da26dd6
refactor: component naming
doodlewind 6a7a384
style: naming
doodlewind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...ages/blocks/src/page-block/edgeless/components/auto-connect/edgeless-auto-connect-line.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { WithDisposable } from '@blocksuite/lit'; | ||
import { css, LitElement, nothing, svg } from 'lit'; | ||
import { customElement, property } from 'lit/decorators.js'; | ||
import { repeat } from 'lit/directives/repeat.js'; | ||
import { styleMap } from 'lit/directives/style-map.js'; | ||
|
||
import { EdgelessBlockType } from '../../../../surface-block/edgeless-types.js'; | ||
import { Bound, type IVec, Vec } from '../../../../surface-block/index.js'; | ||
import type { SurfaceBlockComponent } from '../../../../surface-block/surface-block.js'; | ||
import { isNoteBlock } from '../../utils/query.js'; | ||
|
||
const { NOTE } = EdgelessBlockType; | ||
|
||
@customElement('edgeless-auto-connect-line') | ||
export class EdgelessAutoConnectLine extends WithDisposable(LitElement) { | ||
static override styles = css` | ||
:host { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
pointer-events: none; | ||
} | ||
`; | ||
@property({ attribute: false }) | ||
surface!: SurfaceBlockComponent; | ||
|
||
@property({ attribute: false }) | ||
show = false; | ||
|
||
protected override firstUpdated(): void { | ||
const { _disposables, surface } = this; | ||
_disposables.add( | ||
surface.viewport.slots.viewportUpdated.on(() => { | ||
this.requestUpdate(); | ||
}) | ||
); | ||
|
||
_disposables.add( | ||
surface.page.slots.blockUpdated.on(({ type, id }) => { | ||
if (type === 'update' && isNoteBlock(surface.pickById(id))) { | ||
this.requestUpdate(); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
protected override render() { | ||
if (!this.show) return nothing; | ||
|
||
const { viewport } = this.surface; | ||
const notes = this.surface.getBlocks(NOTE).filter(note => !note.hidden); | ||
const points: [IVec, IVec][] = []; | ||
for (let i = 1; i < notes.length; i++) { | ||
const last = notes[i - 1]; | ||
const current = notes[i]; | ||
const lastBound = Bound.deserialize(last.xywh); | ||
const currentBound = Bound.deserialize(current.xywh); | ||
const start = viewport.toViewCoord(lastBound.center[0], lastBound.maxY); | ||
const end = viewport.toViewCoord( | ||
currentBound.center[0], | ||
currentBound.maxY | ||
); | ||
points.push([start, end]); | ||
} | ||
const expand = 20; | ||
|
||
return repeat( | ||
points, | ||
(_, index) => index, | ||
([start, end]) => { | ||
const width = Math.abs(start[0] - end[0]); | ||
const height = Math.abs(start[1] - end[1]); | ||
const style = styleMap({ | ||
position: 'absolute', | ||
transform: `translate(${Math.min(start[0], end[0]) - expand / 2}px, ${ | ||
Math.min(start[1], end[1]) - expand / 2 | ||
}px)`, | ||
}); | ||
const lineStart = [0, 0]; | ||
const lineEnd = [width, height]; | ||
if (start[0] > end[0]) { | ||
lineStart[0] = width; | ||
lineEnd[0] = 0; | ||
} else { | ||
lineStart[0] = 0; | ||
lineEnd[0] = width; | ||
} | ||
|
||
if (start[1] > end[1]) { | ||
lineStart[1] = height; | ||
lineEnd[1] = 0; | ||
} else { | ||
lineStart[1] = 0; | ||
lineEnd[1] = height; | ||
} | ||
|
||
const newWidth = width + expand; | ||
const newHeight = height + expand; | ||
|
||
const newStart = Vec.add(Vec.pointOffset(lineStart, lineEnd, 16), [ | ||
expand / 2, | ||
expand / 2, | ||
]); | ||
const newEnd = Vec.add(Vec.pointOffset(lineEnd, lineStart, 16), [ | ||
expand / 2, | ||
expand / 2, | ||
]); | ||
|
||
return svg` | ||
<svg style=${style} width="${newWidth}px" height="${newHeight}px" viewBox="0 0 ${newWidth} ${newHeight}" xmlns="http://www.w3.org/2000/svg"> | ||
<defs> | ||
<marker | ||
id="arrow" | ||
refX="10" | ||
refY="10" | ||
markerWidth="10" | ||
markerHeight="20" | ||
orient="auto" | ||
> | ||
<path d="M 2 2 L 10 10 L 2 18" fill="none" stroke="var(--affine-black-10)" stroke-linecap="round" stroke-linejoin="round" /> | ||
</marker> | ||
</defs> | ||
|
||
<line | ||
x1="${newStart[0]}" | ||
y1="${newStart[1]}" | ||
x2="${newEnd[0]}" | ||
y2="${newEnd[1]}" | ||
stroke="var(--affine-black-10)" | ||
stroke-width="2" | ||
marker-end="url(#arrow)" | ||
/> | ||
</svg> | ||
`; | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a const defined in top level of this module?