myPlugin.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import fetch from "node-fetch";
  2. // myPlugin.js
  3. export default function myPlugin() {
  4. return {
  5. name: 'myplugin',
  6. enforce: 'pre',
  7. transform(code, id) {
  8. if (!/\.vue$/.test(id) || !isTargetFile(id)) return
  9. // 为el-col el-row 注入
  10. // :_material="{
  11. // id:'__auto_3_47f3v9'
  12. // }">
  13. let uuid = generateNumericUUID();
  14. code = materialUUID(code)
  15. // 发送处理后的代码到指定接口(非阻塞)
  16. fetch('http://localhost:8080/test/getcode', {
  17. method: 'POST',
  18. headers: { 'Content-Type': 'text/plain' },
  19. body: code // 发送最终处理后的代码
  20. }).catch(error => {
  21. console.error('发送代码失败:', error);
  22. });
  23. console.log(code,2222222)
  24. // 处理模板
  25. const templateContent = extractTemplateContent(code)
  26. const processedTemplate = processTemplate(templateContent)
  27. // 处理脚本
  28. const scriptContent = extractScriptContent(code)
  29. const processedScript = processScript(scriptContent)
  30. // 处理样式
  31. const processedCode = code
  32. .replace(regex, processedTemplate)
  33. .replace(/(<script\s+setup[^>]*>)/, `$1\n${processedScript}`)
  34. .replace(/(<\/style>)/, `${targetStyles}\n$1`)
  35. //输出代码
  36. // console.log(processedCode,1111111)
  37. return processedCode
  38. },
  39. handleHotUpdate(ctx) {
  40. const read = ctx.read
  41. if (/\.vue$/.test(ctx.file)) {
  42. ctx.read = async () => {
  43. const code = await read()
  44. return `${code}`
  45. }
  46. }
  47. }
  48. }
  49. }
  50. const regex = /<!--watchstart-->([\s\S]*?)<!--watchend-->/
  51. // 新增方法:生成_material属性并拼接标签
  52. function materialUUID(code) {
  53. return code.replace(/<el-col([^>]*)>/g, (match, attrs) => {
  54. const uid = generateNumericUUID(); // 生成纯数字UUID
  55. // 构建_material属性字符串
  56. const materialAttr = ` :_material="{id: '${uid}'}"`;
  57. // 将新属性拼接到原有属性后面
  58. return `<el-col${attrs}${materialAttr}>`;
  59. });
  60. }
  61. // 模板处理逻辑
  62. function processTemplate(template) {
  63. let rowIndex = 0
  64. return template
  65. .replace(/<el-row/g, (match) => {
  66. rowIndex++
  67. return `${match} data-auto-row="${rowIndex}"`
  68. })
  69. .replace(/<el-col([^>]*)>/g, (match, attrs) => {
  70. const idMatch = attrs.match(/:_material="\{id:\s*'(\d+)'\}"/);
  71. const uid = idMatch ? idMatch[1] : generateNumericUUID(); // 使用已有ID或生成备用ID
  72. return `
  73. <el-col${attrs}>
  74. <div class="grid-content"
  75. id="${uid}"
  76. draggable="true"
  77. @dragstart="__autoDragStart($event, '${uid}')"
  78. @dragover="__autoDragOver($event, '${uid}')"
  79. @dragleave="__autoDragLeave"
  80. @drop="__autoDragDrop($event, '${uid}')">
  81. <div class="drop-hint left"
  82. v-if="__autoDropInfo.targetId === '${uid}'
  83. && __autoDropInfo.direction === 'left'"></div>
  84. <div class="drop-hint right"
  85. v-if="__autoDropInfo.targetId === '${uid}'
  86. && __autoDropInfo.direction === 'right'"></div>
  87. `.trim().replace(/\n\s+/g, ' ')
  88. })
  89. .replace(/<\/el-col>/g, '</div></el-col>')
  90. }
  91. // 脚本处理逻辑
  92. function processScript(script) {
  93. const autoScript = `
  94. // AUTO-INJECTED DRAG LOGIC
  95. const __autoDragElement = ref(null)
  96. const __autoDropInfo = ref({
  97. targetId: null,
  98. direction: null
  99. })
  100. const __autoDragStart = (e, id) => {
  101. __autoDragElement.value = id
  102. e.dataTransfer.effectAllowed = 'move'
  103. }
  104. const __autoDragOver = (e, targetId) => {
  105. e.preventDefault()
  106. const rect = e.currentTarget.getBoundingClientRect()
  107. __autoDropInfo.value = {
  108. targetId,
  109. direction: e.clientX - rect.left < rect.width / 2 ? 'left' : 'right'
  110. }
  111. }
  112. const __autoDragLeave = () => {
  113. __autoDropInfo.value = { targetId: null, direction: null }
  114. }
  115. const __autoDragDrop = (e, targetId) => {
  116. e.preventDefault()
  117. if (!__autoDragElement.value || !targetId) return
  118. // 构造请求数据
  119. const data = \`\${__autoDragElement.value}-\${targetId}-\${__autoDropInfo.value.direction}\`;
  120. // 发送 POST 请求
  121. fetch('http://localhost:8080/test/changecode', {
  122. method: 'POST',
  123. headers: {
  124. 'Content-Type': 'application/x-www-form-urlencoded', // 或 'text/plain' 根据后端需求
  125. },
  126. body: data
  127. })
  128. .catch(error => {
  129. console.error('请求失败:', error);
  130. });
  131. __autoDragElement.value = null
  132. __autoDropInfo.value = { targetId: null, direction: null }
  133. }
  134. `.trim()
  135. return script.includes('ref')
  136. ? autoScript
  137. : `import { ref } from 'vue'\n${autoScript}`
  138. }
  139. // 辅助函数
  140. function isTargetFile(id) {
  141. return id === 'D:/workspace/RuoYi-Vue/ruoyi-ui/src/views/index.vue'
  142. }
  143. function extractTemplateContent(code) {
  144. const match = code.match(regex)
  145. return match ? match[1] : ''
  146. }
  147. function extractScriptContent(code) {
  148. const scriptMatch = code.match(/<script\s+setup[^>]*>([\s\S]*?)<\/script>/)
  149. return scriptMatch ? scriptMatch[1] : ''
  150. }
  151. // 样式配置(保持原样)
  152. //样式
  153. const targetStyles = `.grid-content {
  154. border: 1px solid #e4e7ed;
  155. min-height: 36px;
  156. display: flex;
  157. align-items: center;
  158. justify-content: center;
  159. padding: 20px;
  160. transition: all 0.3s ease;
  161. position: relative;
  162. cursor: move;
  163. user-select: none;
  164. background: white;
  165. z-index: 1;
  166. /* 悬停效果 */
  167. &:hover {
  168. background-color: #f5f7fa;
  169. border-color: #409eff;
  170. transform: translateY(-2px);
  171. z-index: 2;
  172. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  173. }
  174. /* 拖放指示器 */
  175. .drop-hint {
  176. position: absolute;
  177. top: 0;
  178. height: 100%;
  179. width: 50%;
  180. z-index: 3;
  181. pointer-events: none;
  182. &::after {
  183. position: absolute;
  184. top: 50%;
  185. transform: translateY(-50%);
  186. font-size: 12px;
  187. color: #409eff;
  188. font-weight: 500;
  189. padding: 4px 8px;
  190. background: rgba(255, 255, 255, 0.9);
  191. border-radius: 4px;
  192. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  193. }
  194. /* 左侧指示 */
  195. &.left {
  196. left: 0;
  197. border-left: 3px solid #409eff;
  198. &::after {
  199. content: "← 放置左侧";
  200. left: 10px;
  201. }
  202. }
  203. /* 右侧指示 */
  204. &.right {
  205. right: 0;
  206. border-right: 3px solid #409eff;
  207. &::after {
  208. content: "放置右侧 →";
  209. right: 10px;
  210. }
  211. }
  212. }
  213. /* 拖放时的半透明效果 */
  214. &[draggable="true"]:active {
  215. opacity: 0.8;
  216. transform: scale(0.98);
  217. }
  218. /* 当前拖拽元素的样式 */
  219. &.drag-overlay {
  220. opacity: 0.5;
  221. transform: rotate(3deg);
  222. }
  223. }
  224. /* 行间距 */
  225. .el-row {
  226. margin-bottom: 20px;
  227. &:last-child {
  228. margin-bottom: 0;
  229. }
  230. }
  231. /* 拖放过程的状态指示 */
  232. .drag-enter {
  233. background: linear-gradient(
  234. 45deg,
  235. rgba(64, 158, 255, 0.1) 25%,
  236. transparent 25%,
  237. transparent 50%,
  238. rgba(64, 158, 255, 0.1) 50%,
  239. rgba(64, 158, 255, 0.1) 75%,
  240. transparent 75%,
  241. transparent
  242. );
  243. background-size: 40px 40px;
  244. animation: dragStripes 3s linear infinite;
  245. }
  246. @keyframes dragStripes {
  247. 0% { background-position: 0 0; }
  248. 100% { background-position: 40px 0; }
  249. }
  250. /* 优化移动端触摸体验 */
  251. @media (hover: none) {
  252. .grid-content {
  253. touch-action: manipulation;
  254. &:active {
  255. transform: scale(0.96);
  256. }
  257. }
  258. }`;
  259. function generateNumericUUID() {
  260. // 时间戳部分 (13位)
  261. const timestamp = Date.now().toString();
  262. // 随机数部分 (10位)
  263. const randomPart = Math.floor(Math.random() * 9e9 + 1e9).toString();
  264. // 组合并添加校验位
  265. const base = timestamp + randomPart;
  266. // 计算简单校验和
  267. const checksum = base.split('').reduce((sum, char) => sum + parseInt(char, 10), 0) % 10;
  268. return base + checksum;
  269. }