myPlugin.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // myPlugin.js
  2. export default function myPlugin() {
  3. return {
  4. name: 'myplugin',
  5. enforce: 'pre',
  6. transform(code, id) {
  7. if (!/\.vue$/.test(id) || !isTargetFile(id)) return
  8. // 处理模板
  9. const templateContent = extractTemplateContent(code)
  10. const processedTemplate = processTemplate(templateContent)
  11. // 处理脚本
  12. const scriptContent = extractScriptContent(code)
  13. const processedScript = processScript(scriptContent)
  14. // 处理样式
  15. const processedCode = code
  16. .replace(regex, processedTemplate)
  17. .replace(/(<script\s+setup[^>]*>)/, `$1\n${processedScript}`)
  18. .replace(/(<\/style>)/, `${targetStyles}\n$1`)
  19. //输出代码
  20. console.log(processedCode,1111111)
  21. return processedCode
  22. },
  23. handleHotUpdate(ctx) {
  24. const read = ctx.read
  25. if (/\.vue$/.test(ctx.file)) {
  26. ctx.read = async () => {
  27. const code = await read()
  28. return `${code}`
  29. }
  30. }
  31. }
  32. }
  33. }
  34. const regex = /<!--watchstart-->([\s\S]*?)<!--watchend-->/
  35. // 模板处理逻辑
  36. function processTemplate(template) {
  37. let rowIndex = 0
  38. return template
  39. .replace(/<el-row/g, (match) => {
  40. rowIndex++
  41. return `${match} data-auto-row="${rowIndex}"`
  42. })
  43. .replace(/<el-col([^>]*)>/g, (match, attrs) => {
  44. const uniqueSuffix = Math.random().toString(36).substr(2, 6)
  45. const uid = `__auto_${rowIndex}_${uniqueSuffix}`
  46. return `
  47. <el-col${attrs}>
  48. <div class="grid-content"
  49. id="${uid}"
  50. draggable="true"
  51. @dragstart="__autoDragStart($event, '${uid}')"
  52. @dragover="__autoDragOver($event, '${uid}')"
  53. @dragleave="__autoDragLeave"
  54. @drop="__autoDragDrop($event, '${uid}')">
  55. <div class="drop-hint left"
  56. v-if="__autoDropInfo.targetId === '${uid}'
  57. && __autoDropInfo.direction === 'left'"></div>
  58. <div class="drop-hint right"
  59. v-if="__autoDropInfo.targetId === '${uid}'
  60. && __autoDropInfo.direction === 'right'"></div>
  61. `.trim().replace(/\n\s+/g, ' ')
  62. })
  63. .replace(/<\/el-col>/g, '</div></el-col>')
  64. }
  65. // 脚本处理逻辑
  66. function processScript(script) {
  67. const autoScript = `
  68. // AUTO-INJECTED DRAG LOGIC
  69. const __autoDragElement = ref(null)
  70. const __autoDropInfo = ref({
  71. targetId: null,
  72. direction: null
  73. })
  74. const __autoDragStart = (e, id) => {
  75. __autoDragElement.value = id
  76. e.dataTransfer.effectAllowed = 'move'
  77. }
  78. const __autoDragOver = (e, targetId) => {
  79. e.preventDefault()
  80. const rect = e.currentTarget.getBoundingClientRect()
  81. __autoDropInfo.value = {
  82. targetId,
  83. direction: e.clientX - rect.left < rect.width / 2 ? 'left' : 'right'
  84. }
  85. }
  86. const __autoDragLeave = () => {
  87. __autoDropInfo.value = { targetId: null, direction: null }
  88. }
  89. const __autoDragDrop = (e, targetId) => {
  90. e.preventDefault()
  91. if (!__autoDragElement.value || !targetId) return
  92. console.log(\`DRAG_ACTION: \${__autoDragElement.value} -> \${targetId} (\${__autoDropInfo.value.direction})\`)
  93. __autoDragElement.value = null
  94. __autoDropInfo.value = { targetId: null, direction: null }
  95. }
  96. `.trim()
  97. return script.includes('ref')
  98. ? autoScript
  99. : `import { ref } from 'vue'\n${autoScript}`
  100. }
  101. // 辅助函数
  102. function isTargetFile(id) {
  103. return id === 'D:/workspace/RuoYi-Vue/ruoyi-ui/src/views/index.vue'
  104. }
  105. function extractTemplateContent(code) {
  106. const match = code.match(regex)
  107. return match ? match[1] : ''
  108. }
  109. function extractScriptContent(code) {
  110. const scriptMatch = code.match(/<script\s+setup[^>]*>([\s\S]*?)<\/script>/)
  111. return scriptMatch ? scriptMatch[1] : ''
  112. }
  113. // 样式配置(保持原样)
  114. //样式
  115. const targetStyles = `.grid-content {
  116. border: 1px solid #e4e7ed;
  117. min-height: 36px;
  118. display: flex;
  119. align-items: center;
  120. justify-content: center;
  121. padding: 20px;
  122. transition: all 0.3s ease;
  123. position: relative;
  124. cursor: move;
  125. user-select: none;
  126. background: white;
  127. z-index: 1;
  128. /* 悬停效果 */
  129. &:hover {
  130. background-color: #f5f7fa;
  131. border-color: #409eff;
  132. transform: translateY(-2px);
  133. z-index: 2;
  134. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  135. }
  136. /* 拖放指示器 */
  137. .drop-hint {
  138. position: absolute;
  139. top: 0;
  140. height: 100%;
  141. width: 50%;
  142. z-index: 3;
  143. pointer-events: none;
  144. &::after {
  145. position: absolute;
  146. top: 50%;
  147. transform: translateY(-50%);
  148. font-size: 12px;
  149. color: #409eff;
  150. font-weight: 500;
  151. padding: 4px 8px;
  152. background: rgba(255, 255, 255, 0.9);
  153. border-radius: 4px;
  154. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  155. }
  156. /* 左侧指示 */
  157. &.left {
  158. left: 0;
  159. border-left: 3px solid #409eff;
  160. &::after {
  161. content: "← 放置左侧";
  162. left: 10px;
  163. }
  164. }
  165. /* 右侧指示 */
  166. &.right {
  167. right: 0;
  168. border-right: 3px solid #409eff;
  169. &::after {
  170. content: "放置右侧 →";
  171. right: 10px;
  172. }
  173. }
  174. }
  175. /* 拖放时的半透明效果 */
  176. &[draggable="true"]:active {
  177. opacity: 0.8;
  178. transform: scale(0.98);
  179. }
  180. /* 当前拖拽元素的样式 */
  181. &.drag-overlay {
  182. opacity: 0.5;
  183. transform: rotate(3deg);
  184. }
  185. }
  186. /* 行间距 */
  187. .el-row {
  188. margin-bottom: 20px;
  189. &:last-child {
  190. margin-bottom: 0;
  191. }
  192. }
  193. /* 拖放过程的状态指示 */
  194. .drag-enter {
  195. background: linear-gradient(
  196. 45deg,
  197. rgba(64, 158, 255, 0.1) 25%,
  198. transparent 25%,
  199. transparent 50%,
  200. rgba(64, 158, 255, 0.1) 50%,
  201. rgba(64, 158, 255, 0.1) 75%,
  202. transparent 75%,
  203. transparent
  204. );
  205. background-size: 40px 40px;
  206. animation: dragStripes 3s linear infinite;
  207. }
  208. @keyframes dragStripes {
  209. 0% { background-position: 0 0; }
  210. 100% { background-position: 40px 0; }
  211. }
  212. /* 优化移动端触摸体验 */
  213. @media (hover: none) {
  214. .grid-content {
  215. touch-action: manipulation;
  216. &:active {
  217. transform: scale(0.96);
  218. }
  219. }
  220. }`;