Giter Site home page Giter Site logo

a7650 / vue3-draggable-resizable Goto Github PK

View Code? Open in Web Editor NEW
592.0 12.0 120.0 637 KB

[Vue3 组件] 用于拖拽调整位置和大小的的组件,同时支持元素吸附对齐,实时参考线。

License: MIT License

JavaScript 5.25% HTML 2.03% Vue 6.41% TypeScript 83.48% CSS 2.84%
vue3 typescript draggable resizable component conflict-detection refline resize drag

vue3-draggable-resizable's Introduction

logo

Vue3DraggableResizable

npm version Software License npm vue version

[Vue3 组件] 用于拖拽调整位置和大小的的组件,同时支持冲突检测,元素吸附对齐,实时参考线。 [ Vue3 Component ] Draggable and resizable component for vue3, and, support element adsorption alignment, real-time reference line, etc.

点击查看中文文档

Table of Contents

Features

  • Draggable and resizable
  • Define handles for resizing
  • Restrict movement and size in parent node
  • Customize various class names
  • Provide your own markup for handles
  • Adsorption alignment
  • Reference line

Usage

$ npm install vue3-draggable-resizable

Register the Vue3DraggableResizable

// >main.js
import { createApp } from 'vue'
import App from './App.vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'

// You will have a global component named "Vue3DraggableResizable"
createApp(App)
  .use(Vue3DraggableResizable)
  .mount('#app')

You can also use it separately within the component

// >component.js
import { defineComponent } from 'vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'

export default defineComponent({
  components: { Vue3DraggableResizable }
  // ...other
})

Here is a complete example of using "vue-template"

<template>
  <div id="app">
    <div class="parent">
      <Vue3DraggableResizable
        :initW="110"
        :initH="120"
        v-model:x="x"
        v-model:y="y"
        v-model:w="w"
        v-model:h="h"
        v-model:active="active"
        :draggable="true"
        :resizable="true"
        @activated="print('activated')"
        @deactivated="print('deactivated')"
        @drag-start="print('drag-start')"
        @resize-start="print('resize-start')"
        @dragging="print('dragging')"
        @resizing="print('resizing')"
        @drag-end="print('drag-end')"
        @resize-end="print('resize-end')"
      >
        This is a test example
      </Vue3DraggableResizable>
    </div>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'
export default defineComponent({
  components: { Vue3DraggableResizable },
  data() {
    return {
      x: 100,
      y: 100,
      h: 100,
      w: 100,
      active: false
    }
  },
  methods: {
    print(val) {
      console.log(val)
    }
  }
})
</script>
<style>
.parent {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 100px;
  left: 100px;
  border: 1px solid #000;
  user-select: none;
}
</style>

Props

initW

type: Number
default: null

Set initial width(px)

<Vue3DraggableResizable :initW="100" />

initH

type: Number
default: null

Set initial height(px)

<Vue3DraggableResizable :initH="100" />

w

type: Number
default: 0

Current width(px) of the container.
You can use "v-model:w" to keeps it up-to-date

<Vue3DraggableResizable v-model:w="100" />

h

type: Number
default: 0

Current height(px) of the container.
You can use "v-model:h" to keeps it up-to-date

<Vue3DraggableResizable v-model:h="100" />

x

type: Number
default: 0

Current left(px) of the container.
You can use "v-model:x" to keeps it up-to-date

<Vue3DraggableResizable v-model:x="100" />

y

type: Number
default: 0

The current top(px) of the container.
You can use "v-model:y" to keeps it up-to-date

<Vue3DraggableResizable v-model:y="100" />

minW

type: Number
default: 20

Minimum width(px)

<Vue3DraggableResizable :minW="100" />

minH

type: Number
default: 20

Minimum height(px)

<Vue3DraggableResizable :minH="100" />

active

type: Boolean
default: false

Indicates whether the component is selected.
You can use "v-model:active" to keeps it up-to-date

<Vue3DraggableResizable v-model:active="100" />

draggable

type: Boolean
default: true

Defines the component can be draggable or not

<Vue3DraggableResizable :draggable="true" />

resizable

type: Boolean
default: true

Defines the component can be resizable or not

<Vue3DraggableResizable :draggable="true" />

lockAspectRatio

type: Boolean
default: false

The lockAspectRatio property is used to lock aspect ratio.

<Vue3DraggableResizable :lockAspectRatio="true" />

disabledX

type: Boolean
default: false

Defines the component can be moved on x-axis or not

<Vue3DraggableResizable :disabledX="true" />

disabledY

type: Boolean
default: false

Defines the component can be moved on y-axis or not

<Vue3DraggableResizable :disabledY="true" />

disabledW

type: Boolean
default: false

Defines the component`s width can be modify or not

<Vue3DraggableResizable :disabledW="true" />

disabledH

type: Boolean
default: false

Defines the component`s height can be modify or not

<Vue3DraggableResizable :disabledH="true" />

parent

type: Boolean
default: false

Restrict movement and size within its parent node

<Vue3DraggableResizable :parent="true" />

handles

type: Array
default: ['tl', 'tm', 'tr', 'ml', 'mr', 'bl', 'bm', 'br']

Define the array of handles to restrict the element resizing

  • tl : Top left
  • tm : Top middle
  • tr : Top right
  • mr : Middle right
  • ml : Middle left
  • bl : Bottom left
  • bm : Bottom middle
  • br : Bottom right
<Vue3DraggableResizable :handles="['tl','tr','bl','br']" />

classNameDraggable

type: String
default: draggable

Used to set the custom class of a draggable-resizable component when draggable is enable.

<Vue3DraggableResizable classNameDraggable="draggable" />

classNameResizable

type: String
default: resizable

Used to set the custom class of a draggable-resizable component when resizable is enable.

<Vue3DraggableResizable classNameResizable="resizable" />

classNameDragging

type: String
default: dragging

Used to set the custom class of a draggable-resizable component when is dragging.

<Vue3DraggableResizable classNameDragging="dragging" />

classNameResizing

type: String
default: resizing

Used to set the custom class of a draggable-resizable component when is resizing.

<Vue3DraggableResizable classNameResizing="resizing" />

classNameActive

type: String
default: active

Used to set the custom class of a draggable-resizable component when is active.

<Vue3DraggableResizable classNameActive="active" />

classNameHandle

type: String
default: handle

Used to set the custom common class of each handle element.

<Vue3DraggableResizable classNameHandle="my-handle" />

following handle nodes will be rendered

...
<div class="vdr-handle vdr-handle-tl my-handle my-handle-tl"></div>
<div class="vdr-handle vdr-handle-tm my-handle my-handle-tm"></div>
<div class="vdr-handle vdr-handle-tr my-handle my-handle-tr"></div>
<div class="vdr-handle vdr-handle-ml my-handle my-handle-mr"></div>
...

Events

activated

payload: -

<Vue3DraggableResizable @activated="activatedHandle" />

deactivated

payload: -

<Vue3DraggableResizable @deactivated="deactivatedHandle" />

drag-start

payload: { x: number, y: number }

<Vue3DraggableResizable @drag-start="dragStartHandle" />

dragging

payload: { x: number, y: number }

<Vue3DraggableResizable @dragging="dragStartHandle" />

drag-end

payload: { x: number, y: number }

<Vue3DraggableResizable @drag-end="dragEndHandle" />

resize-start

payload: { x: number, y: number, w: number, h: number }

<Vue3DraggableResizable @resize-start="resizeStartHandle" />

resizing

payload: { x: number, y: number, w: number, h: number }v

<Vue3DraggableResizable @resizing="resizingHandle" />

resize-end

payload: { x: number, y: number, w: number, h: number }

<Vue3DraggableResizable @resize-end="resizeEndHandle" />

Use-adsorption-alignment

You need to import another component to use the "adsorption alignment" feature.

This can be used as follows.

<template>
  <div id="app">
    <div class="parent">
      <DraggableContainer>
        <Vue3DraggableResizable>
          Test
        </Vue3DraggableResizable>
        <Vue3DraggableResizable>
          Another test
        </Vue3DraggableResizable>
      </DraggableContainer>
    </div>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
// This component is not exported by default
// If you used "app.use(Vue3DraggableResizable)",then you don't need to import it, you can use it directly.
import { DraggableContainer } from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'
export default defineComponent({
  components: { Vue3DraggableResizable, DraggableContainer }
})
</script>
<style>
.parent {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 100px;
  left: 100px;
  border: 1px solid #000;
  user-select: none;
}
</style>

DraggableContainer Props

These props apply to DraggableContainer

disabled

type: Boolean
default: false

Disable this feature

<DraggableContainer :disabled="true">
  <Vue3DraggableResizable>
    Test
  </Vue3DraggableResizable>
  <Vue3DraggableResizable>
    Another test
  </Vue3DraggableResizable>
</DraggableContainer>

adsorbParent

type: Boolean
default: true

Adsorption near parent component

<DraggableContainer :adsorbParent="false">
  <Vue3DraggableResizable>
    Test
  </Vue3DraggableResizable>
  <Vue3DraggableResizable>
    Another test
  </Vue3DraggableResizable>
</DraggableContainer>

adsorbCols

type: Array<Number>
default: null

Custom guides(column)

<DraggableContainer :adsorbCols="[10,20,30]">
  <Vue3DraggableResizable>
    Test
  </Vue3DraggableResizable>
  <Vue3DraggableResizable>
    Another test
  </Vue3DraggableResizable>
</DraggableContainer>

adsorbRows

type: Array<Number>
default: null

Custom guides(row)

<DraggableContainer :adsorbRows="[10,20,30]">
  <Vue3DraggableResizable>
    Test
  </Vue3DraggableResizable>
  <Vue3DraggableResizable>
    Another test
  </Vue3DraggableResizable>
</DraggableContainer>

referenceLineVisible

type: Boolean
default: true

reference line visible

<DraggableContainer :referenceLineVisible="false">
  <Vue3DraggableResizable>
    Test
  </Vue3DraggableResizable>
  <Vue3DraggableResizable>
    Another test
  </Vue3DraggableResizable>
</DraggableContainer>

referenceLineColor

type: String
default: #f00

reference line color

<DraggableContainer :referenceLineColor="#0f0">
  <Vue3DraggableResizable>
    Test
  </Vue3DraggableResizable>
  <Vue3DraggableResizable>
    Another test
  </Vue3DraggableResizable>
</DraggableContainer>

vue3-draggable-resizable's People

Contributors

a7650 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue3-draggable-resizable's Issues

[Question] How can I get Parent Width and Parent Height

In VueDraggableResizable component, the function setup() will return a parameter ...parentSize that contains parentWidth and parentHeight. How can I access this two parameters?

I tried adding ref="root" into the <Vue3DraggableResizable/> and access using root.parentWidth, but was unccessful.

Thanks in advance!

Unable to import into Vue3

Hi,

I come across this plugin and it is very easy to understand the usage.

I installed this plugin for my Vue3 project using

npm install vue3-draggable-resizable

The install step is successful.
Then I import it into my main.js . My main.js script looks like this:

import { createApp } from 'vue'
import App from './App.vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'
createApp(App).use(Vue3DraggableResizable).mount('#app')

I then run npm run serve in terminal. But I encountered this error:

 ERROR  Failed to compile with 2 errors                                                                                                     1:12:56 AM

These relative modules were not found:

* ./components/DraggableContainer in ./node_modules/vue3-draggable-resizable/src/index.js
* ./components/Vue3DraggableResizable in ./node_modules/vue3-draggable-resizable/src/index.js

Is there a way to solve this? Thanks!
Feel free ask for more info if you cannot replicate this error.

vue warns about the 'identity' injection not being found

[Vue warn]: injection "identity" not found.

and the code i m using:

<Vue3DraggableResizable
    class="editor-console"
    :initW="110"
    :initH="120"
    v-model:active="active"
    :draggable="false"
    :resizable="true"
  >
    This is a test example
  </Vue3DraggableResizable>

Support touch device

Hello there, thank you for the great work!
I'm currently using this library in my Vue 3 project, it works fine on desktop device with mouse/touchpad, but it would be better if the draggable/resizable support touch device!

组件激活状态变为非活跃状态

你好,在Vue3DraggableResizable里,active跟它的绑定值并没有关系啊,只要是点击这个组件外的位置都会变成非活跃状态,我要在旁边编辑修改点击组件的属性,需要保持活跃状态,请问能尽快修复一下这个问题吗,谢谢

嵌套问题

请问:当vue3-draggable-resizable 中继续嵌套一个vue3-draggable-resizable时, 内层拖动会影响外层一起动,有什么解决方案吗?
万分感谢!

Auto sizing feature ?

Hi there, I've been playing a bit with your library and I've found it very smartly designed and versatile, but I'm missing a feature that I am not sure if is available.

Is there any way to enable automatic sizing for width and height, according to the inner contents?

I'm dynamically showing and hiding some parts of the contents injected inside the Vue3DraggableResizable component, and I would need the container to react to the new area, but I found no way so far.
Is such a feature covered by the library?

加个scoped?

<script lang="ts"> import d from "./Vue3DraggableResizable"; export default d; </script> <style lang="scss" scoped> @import "./index.css"; </style>

是否可以增加监听手柄的事件

您好,遇到了这样的一个需求,烦请指导~~

  1. 拖动四个拐角的手柄,改变宽高比例。
  2. 拖动上下轴线上的手柄,只改变高度。
  3. 拖动左右轴线上的手柄,只改变宽度。
    初步思路是:想通过监听控制手柄的事件,改变lockAspectRatio,但目前好像无法监测到resizing时用得哪个手柄?

我用Usage里面的样例

报错显示:
These relative modules were not found:

  • ./components/DraggableContainer in ./node_modules/vue3-draggable-res-resizable/src/index.jsizable/src/index.js
  • ./components/Vue3DraggableResizable in ./node_modules/vue3-draggable-resizable/src/index.js

谢谢

Hello, I hope you can add new features

Hello, you can add vue3 draggable resizable when zooming the editing area. Can you also zoom at the same time? Because it's very inconvenient for me to wear w h and X Y times scale. Another problem is that when the parent is true, the values of W, h, x, y cannot be obtained. Also, can I add a reference line when pulling the size? For example, when it is pulled to the same size, and when the X and Y axes are in the same position. Thank you

优化功能

  • 增加网格(插件本身没有直接提供网格的配置,但使用者可以借助插件内的DraggableContainer组件进行自定义参考线,从而实现网格)

  • 等比缩放

  • 宽/高/x/y通过参数配置是否可修改

  • 冲突检测/吸附对齐(通过容器依赖注入的方式),可配置吸附范围。

Vue3.2版本引入vue3-draggable-resizable组件出现大量警告。

1、[Vue warn]: injection "identity" not found.
2、[Vue warn]: setup() return property "$setWidth" should not start with "$" or "" which are reserved prefixes for Vue internals
3、[Vue warn]: setup() return property "$setHeight" should not start with "$" or "
" which are reserved prefixes for Vue internals.

1.6.1 版本

最新版本,项目跑起来
These relative modules were not found:

  • ./components/DraggableContainer in ./node_modules/vue3-draggable-resizable/src/index.js
  • ./components/Vue3DraggableResizable in ./node_modules/vue3-draggable-resizable/src/index.js
    components里面的js文件跟上个版本比较都没有了

使用 typescript + jsx 时,声明 callback 无法通过类型检查

比如:

<script lang="tsx">
import { defineComponent } from 'vue';
import Vue3DraggableResizable from 'vue3-draggable-resizable';
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css';

export default defineComponent({
  methods: {
    onResizeEnd() {},
  },
  render() {
    return (
      <Vue3DraggableResizable
        onResizeEnd={this.onResizeEnd}
      >
        <div />
      </Vue3DraggableResizable>
    );
  },
});
</script>

会报错:

TS2322: Type '{ class: string; classNameResizing: string; initH: any; h: any; draggable: false; active: boolean; handles: string[]; onDeactivated: () => void; }' is not assignable to type 'IntrinsicAttributes & Partial<{ active: boolean; x: number; y: number; w: number; h: number; draggable: boolean; resizable: boolean; initW: number; initH: number; disabledX: boolean; ... 13 more ...; lockAspectRatio: boolean; }> & Pick<...> & Partial<...> & Pick<...>'.
  Property 'onDeactivated' does not exist on type 'IntrinsicAttributes & Partial<{ active: boolean; x: number; y: number; w: number; h: number; draggable: boolean; resizable: boolean; initW: number; initH: number; disabledX: boolean; ... 13 more ...; lockAspectRatio: boolean; }> & Pick<...> & Partial<...> & Pick<...>'.
  > 92 |             onDeactivated={this.onDeactivated}
       |             ^^^^^^^^^^^^^

vue3-draggable-resizable\typings\index.d.ts 的每一块声明中添加

onDeactivated: (...args: any[]) => any;

可以解决,每个事件可能都需要加一下。

warnings for props starting with "$"

When I create a new Vue3DraggableResizable component as described in the README

    <Vue3DraggableResizable
      :initW="110"
      :initH="120"
      v-model:x="x"
      v-model:y="y"
      v-model:w="w"
      v-model:h="h"
      v-model:active="active"
      :draggable="true"
      :resizable="true"
      @activated="print('activated')"
      @deactivated="print('deactivated')"
      @drag-start="print('drag-start')"
      @resize-start="print('resize-start')"
      @dragging="print('dragging')"
      @resizing="print('resizing')"
      @drag-end="print('drag-end')"
      @resize-end="print('resize-end')"
    >

I get multiple warnings:

Screenshot_2021-01-04_18-01-14

The first one has been already addressed in #2, but for the other ones I think there is no fix yet

This is my package.json:

Screenshot_2021-01-04_18-01-56

希望支持drag handle

dragHandle
Type: String
Required: false

Defines the selector that should be used to drag the component.

<vue3-draggable-resizable drag-handle=".drag">

引入错误

在单个页面引入:
import Vue3DraggableResizable from 'vue3-draggable-resizable'
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'

报错:
These relative modules were not found:
./components/DraggableContainer in ./node_modules/vue3-draggable-resizable/src/index.js
./components/Vue3DraggableResizable in ./node_modules/vue3-draggable-resizable/src/index.js

vue3引入报错

按照组件引入方式报下列错误
"identity" not found.
at
setup() return property "$setTop" should not start with "$" or "_" which are reserved prefixes for Vue internals.

1.拖拽角标bug【tm,tr,ml,mr,bl】。

1.拖拽角标bug【tm,tr,ml,mr,bl】。位置和理想效果不对。
自己根据源码修改的不知道,其他地方是否有影响。
解决修改代码hooks.js两处:
一:
// hooks.js 355 if (idx0 === 't') { if(idx1 === 'l'){ setWidth(lstW - deltaX); setLeft(lstX - (width.value - lstW)); }else if(idx1 === 'r'){ setWidth(lstW + deltaX); } setHeight(lstH - deltaY); setTop(lstY - (height.value - lstH)); } else if (idx0 === 'b') { setHeight(lstH + deltaY); if(idx1 === 'l'){ setWidth(lstW - deltaX); setLeft(lstX - (width.value - lstW)); }else if(idx1 === 'r'){ setWidth(lstW + deltaX); } } else{ if (idx1 === 'l'){ setWidth(lstW - deltaX); setLeft(lstX - (width.value - lstW)); }else{ setWidth(lstW + deltaX); } }
二:
// hooks.js 415 if (aspectRatio.value) { idx0 = handleType.slice(0,1); idx1 = handleType.slice(1,2); }

Ps:不知道对其他地方有没有影响,第一次用GitHub,不太会使用,反馈应该有回复吧,不知道有没有留言私信。

在缩放的过程中有莫名其妙的卡克

在左右上下缩放的过程中有莫名其妙的卡克感,缩放到相对较小的时候,就开始卡克,不能一次性缩放到最小,要经历多次缩放才可以到最小

Grid movement

Really loving this library! Would it be possible to add grid-movement however? So you can add prop like :grid="[5, 5]" so it moves in a 5x5 pixels grid. So you have to drag the mouse 5 pixels in any direction for it to move

Thanks!

Support Rotatable

Hello team, you are doing amazing!

I know this library is focus on draggable + resizable, but since there is no rotatable lib that support Vue 3 yet, it would be great if this lib can. You can provide a new prop, like :rotatable="true", or create a new library that extends from this library. Please consider this!

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.