DmitrMakeev commited on
Commit
f12ddc3
·
verified ·
1 Parent(s): e44859a

Create per.js

Browse files
Files changed (1) hide show
  1. js/per.js +70 -0
js/per.js ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import 'grapesjs/dist/css/grapes.min.css'
2
+
3
+ import grapesjs, { usePlugin } from 'grapesjs'
4
+ import grapesjsBlocks from 'grapesjs-blocks-basic'
5
+
6
+ import type { Editor, Component } from 'grapesjs'
7
+
8
+ function runExample () {
9
+ const protectedCss: string = `
10
+ * {
11
+ box-sizing: border-box;
12
+ margin: 0;
13
+ padding: 0;
14
+ }
15
+ html, body, [data-gjs-type="wrapper"] {
16
+ width: 100%;
17
+ height: 100%;
18
+ }
19
+ `
20
+ const editor = grapesjs.init({
21
+ container: '#gjs', // same as id in the "index.html" file
22
+ height: '100vh',
23
+ canvas: {
24
+ infiniteCanvas: true
25
+ },
26
+ fromElement: true,
27
+ storageManager: false,
28
+ protectedCss,
29
+ plugins: [
30
+ usePlugin(grapesjsBlocks, {
31
+ flexGrid: true
32
+ })
33
+ ]
34
+ })
35
+
36
+ let isDragging = false;
37
+ let offsetX = 0;
38
+ let offsetY = 0;
39
+ let selectedComponent: Component | null = null;
40
+
41
+ function startDrag(e: MouseEvent, component: Component) {
42
+ isDragging = true;
43
+ selectedComponent = component;
44
+ const rect = component.getEl().getBoundingClientRect();
45
+ offsetX = e.clientX - rect.left;
46
+ offsetY = e.clientY - rect.top;
47
+ document.addEventListener('mousemove', drag);
48
+ document.addEventListener('mouseup', stopDrag);
49
+ }
50
+
51
+ function drag(e: MouseEvent) {
52
+ if (!isDragging || !selectedComponent) return;
53
+ const newX = e.clientX - offsetX;
54
+ const newY = e.clientY - offsetY;
55
+ selectedComponent.set('position', { x: newX, y: newY });
56
+ }
57
+
58
+ function stopDrag() {
59
+ isDragging = false;
60
+ selectedComponent = null;
61
+ document.removeEventListener('mousemove', drag);
62
+ document.removeEventListener('mouseup', stopDrag);
63
+ }
64
+
65
+ editor.on('component:selected', (component: Component) => {
66
+ component.getEl().addEventListener('mousedown', (e: MouseEvent) => startDrag(e, component));
67
+ });
68
+ }
69
+
70
+ runExample()