forked from blocks/blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransforms.js
More file actions
138 lines (120 loc) · 3.95 KB
/
Copy pathtransforms.js
File metadata and controls
138 lines (120 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { transform } from '@babel/standalone'
import babelPluginTransformJsx from '@babel/plugin-transform-react-jsx'
import babelPluginSyntaxJsx from '@babel/plugin-syntax-jsx'
import babelPluginSetDefaultExportToContainer from './babel-plugins/set-default-export-to-container'
import babelPluginAddTuid from './babel-plugins/add-tuid-prop'
import babelPluginRemoveNamedExports from './babel-plugins/remove-named-exports'
import babelPluginDnd from './babel-plugins/drag-and-drop'
import babelPluginRemoveTuid from './babel-plugins/remove-tuid'
import babelPluginReorderBlocks from './babel-plugins/reorder-blocks'
import babelPluginApplySxProp from './babel-plugins/apply-sx-prop'
import babelPluginApplyProp from './babel-plugins/apply-prop'
import babelPluginInjectBlocksRoot from './babel-plugins/inject-blocks-root'
import babelPluginRemoveImports from './babel-plugins/remove-imports'
import babelPluginRemoveSxProp from './babel-plugins/remove-sx-prop'
import babelPluginRemove from './babel-plugins/remove'
import babelPluginInsertBefore from './babel-plugins/insert-before'
import babelPluginInsertAfter from './babel-plugins/insert-after'
import babelPluginClone from './babel-plugins/clone'
import babelPluginInsertBlock from './babel-plugins/insert-block'
import babelPluginReplaceText from './babel-plugins/replace-text'
const transformPlugins = [
babelPluginSetDefaultExportToContainer,
babelPluginDnd,
babelPluginInjectBlocksRoot,
babelPluginRemoveNamedExports,
babelPluginRemoveImports,
[
babelPluginTransformJsx,
{
pragma: 'jsx'
}
]
]
export const toTransformedJSX = code => {
try {
return transform(code, {
plugins: transformPlugins
}).code
} catch (e) {
return null
}
}
export const toTransformedBlockJSX = code => {
const fullCode = 'export default () => ' + code.trim()
return transform(fullCode, {
plugins: [
babelPluginRemoveImports,
babelPluginSetDefaultExportToContainer,
[
babelPluginTransformJsx,
{
pragma: 'jsx'
}
]
]
}).code
}
export const toRawJSX = code => {
try {
return transform(code, {
plugins: [babelPluginSyntaxJsx, babelPluginRemoveTuid]
}).code
} catch (e) {
return null
}
}
export const addTuid = code =>
transform(code, {
plugins: [babelPluginSyntaxJsx, babelPluginAddTuid]
}).code
export const applySxProp = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginApplySxProp, options]]
})
export const removeSxProp = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginRemoveSxProp, options]]
})
export const cloneElement = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginClone, options]]
})
export const removeElement = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginRemove, options]]
})
export const replaceText = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginReplaceText, options]]
})
export const insertElementBefore = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginInsertBefore, options]]
})
export const insertElementAfter = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginInsertAfter, options]]
})
export const applyProp = (code, options = {}) =>
transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginApplyProp, options]]
})
export const reorderJSXBlocks = (code, drag) => {
return transform(code, {
plugins: [babelPluginSyntaxJsx, [babelPluginReorderBlocks, drag]]
}).code
}
export const insertJSXBlock = (code, { blocks, ...drag }) => {
const block = blocks[drag.draggableId]
if (!block) {
return
}
return transform(code, {
plugins: [
babelPluginSyntaxJsx,
[babelPluginInsertBlock, { ...drag, block }],
babelPluginAddTuid
]
}).code
}