Skip to content
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: draggable element #1402

Merged
merged 50 commits into from Apr 10, 2024
Merged

feat: draggable element #1402

merged 50 commits into from Apr 10, 2024

Conversation

KermanX
Copy link
Member

@KermanX KermanX commented Mar 9, 2024

This PR implements a <v-drag> component:

<v-drag pos="451,398,239,48">
draggable box
<img ... >
</v-drag>

Outdated. Please check the docs for the usage.

Then you can double-click the element in the slide, and then drag it to resize and move it. The pos prop will be auto-updated. Clicking outside the currently active fixed element will stop the dragging.

Initially, you don't need to specify the pos prop, which will be auto-generated on the first drag.

The format of pos is left,top,width,height,rotate. rotate is omitted when it is 0.

Press shift when resizing, the aspect ratio will be fixed.

You can also use arrow keys to move the element.

image

About HMR

In the following situations, the new position will be written back to the Markdown:

  • Dragged && debounce(500)
  • Click outside the element to stop dragging
  • The window lost its focus

However, there are still some restrictions:

  • When dragging pos isn't saved yet, modifying the Markdown file via the external editor will cause UB. In most cases, this would not happen because the window will lose its focus when you switch to the code editor.
  • Multi-user editing at the same time will cause UB.

Todos

  • Documentation. I will work on this after the functions get approved.
  • Find a way to support <v-drag ... in code block or comment. Currently, the writing back is based on the index of regex match.
  • Allow width and height to be auto.

Notes

It would be better if we could use v-fixed as a directive, but I don't know how to do this.

The behavior is mostly learned from Figma.

Copy link

netlify bot commented Mar 9, 2024

Deploy Preview for slidev ready!

Name Link
🔨 Latest commit 44f04e9
🔍 Latest deploy log https://app.netlify.com/sites/slidev/deploys/6615f42d7293630008371631
😎 Deploy Preview https://deploy-preview-1402--slidev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@KermanX KermanX marked this pull request as ready for review March 16, 2024 05:02
@KermanX KermanX changed the title feat: fixed&resizable elements feat: draggable element Mar 17, 2024
packages/slidev/node/vite/loaders.ts Outdated Show resolved Hide resolved
packages/client/composables/useDragElements.ts Outdated Show resolved Hide resolved
@KermanX
Copy link
Member Author

KermanX commented Mar 26, 2024

Is there any possibility we can use VDrag as a directive? It seems that directives cannot render new elements...

@antfu
Copy link
Member

antfu commented Mar 29, 2024

Yeah I was thinking about that, having a directive would be nice but I guess it will not be easy to do. We could leave it for the future

@antfu antfu added this to the 0.49 milestone Apr 10, 2024
@antfu antfu merged commit 65a1cda into slidevjs:main Apr 10, 2024
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants