index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import { computed, defineComponent, onMounted, reactive, Transition, nextTick, watch } from "vue";
  2. import state, { EnumMusicRenderType, handleSelection, skipNotePlay, IPlatform, resetBaseRate } from "/src/state";
  3. import styles from "./index.module.less";
  4. import { metronomeData } from "/src/helpers/metronome";
  5. import { evaluatingData } from "../evaluating";
  6. import { leveByScoreMeasureIcons } from "../evaluating/evaluatResult";
  7. import { Icon, showToast } from "vant";
  8. import MoveMusicScore, { moveData, renderForMoveData } from "../plugins/move-music-score";
  9. import { useRoute } from "vue-router";
  10. import { getQuery } from "/src/utils/queryString";
  11. import IntonationDown from "./imgs/pitchLow.png"
  12. import IntonationUp from "./imgs/pitchHigh.png"
  13. import MultipleRestMeasures from "./multipleRestMeasures"
  14. import { browser } from "../../utils";
  15. import { transform } from "lodash";
  16. export default defineComponent({
  17. name: "selection",
  18. setup() {
  19. const browsInfo = browser();
  20. const isPad = navigator?.userAgent?.includes("UAWEIVRD-W09") || browsInfo?.iPad || browsInfo.isTablet;
  21. const route = useRoute();
  22. const query: any = {
  23. ...getQuery(),
  24. ...route.query,
  25. };
  26. const selectData = reactive({
  27. notes: [] as any[],
  28. staves: [] as any[],
  29. measureHeight: 0 as number, // 小节高度
  30. // beatWidth: '100%' as string, // 节拍指针占用的宽度,带有拍号的小节,节拍指针占用的宽度需要减去拍号左侧的宽度
  31. // beatLeft: 0 as number, // 小节有拍号时,拍号左侧宽度
  32. });
  33. const beatMeasureWidths: any = {}; // 节拍指针需要占用的宽度
  34. /** 计算点击层数据 */
  35. const calcNoteData = () => {
  36. const musicContainer = document.getElementById("musicAndSelection")?.getBoundingClientRect() || {
  37. x: 0,
  38. y: 0,
  39. };
  40. const parentLeft = musicContainer.x || 0;
  41. const parentTop = musicContainer.y || 0;
  42. const notes = state.times;
  43. const notesList: string[] = [];
  44. const MeasureNumberXMLList: number[] = [];
  45. let minMeasureHeigt: number = 0;
  46. for (let i = 0; i < notes.length; i++) {
  47. const item = notes[i];
  48. // console.log("🚀 ~ item:", item)
  49. const noteItem = {
  50. ...item,
  51. index: item.i,
  52. bbox: null as any,
  53. staveBox: null as any,
  54. };
  55. if (!notesList.includes(item.noteId)) {
  56. let staveBbox: any = {}, customBgBox: any = {};
  57. if (item.stave?.attrs?.id) {
  58. const staveEle = document.querySelector(`#${item.stave.attrs.id}`);
  59. staveBbox = staveEle?.parentElement?.parentElement?.getBoundingClientRect?.() || {
  60. x: 0,
  61. width: 0,
  62. };
  63. customBgBox = staveEle?.querySelector('.vf-custom-bg')?.getBoundingClientRect() || { y: 0, height: 0 }
  64. // console.log("🚀 ~ staveBbox:", staveBbox.height)
  65. }
  66. if (item.svgElement) {
  67. const noteEle = document.querySelector(`#vf-${item.svgElement?.attrs?.id}`);
  68. if (noteEle) {
  69. const noteBbox = noteEle.getBoundingClientRect?.() || { x: 0, width: 0 };
  70. if (state.musicRenderType !== EnumMusicRenderType.staff) {
  71. noteItem.bbox = {
  72. left: noteBbox.x - parentLeft - noteBbox.width / 4 + "px",
  73. top: noteBbox.y - parentTop - noteBbox.height + "px",
  74. width: noteBbox.width * 1.5 + "px",
  75. height: noteBbox.height * 3 + "px",
  76. x: item.bbox?.x,
  77. y: item.bbox?.y,
  78. originWidth: item.bbox?.width
  79. };
  80. const noteHead = noteEle.querySelector(".vf-numbered-note-head");
  81. const noteHeadBbox = noteHead?.getBoundingClientRect?.();
  82. if (noteHeadBbox) {
  83. item.bbox = {
  84. left: noteHeadBbox.x - parentLeft - noteHeadBbox.width / 4,
  85. width: noteHeadBbox.width * 1.5,
  86. x: item.bbox?.x,
  87. y: item.bbox?.y,
  88. originWidth: item.bbox?.width
  89. }
  90. }
  91. } else {
  92. const needTransY = -(staveBbox.height - customBgBox.height) / 2 + "px";
  93. noteItem.bbox = {
  94. left: noteBbox.x - parentLeft - noteBbox.width / 4 + "px",
  95. top: customBgBox.y ? customBgBox.y - parentTop + "px" : staveBbox.y - parentTop + "px",
  96. width: noteBbox.width * 1.5 + "px",
  97. height: staveBbox.height + "px",
  98. x: item.bbox?.x,
  99. y: item.bbox?.y,
  100. originWidth: item.bbox?.width,
  101. transform: `translateY(${needTransY})`
  102. };
  103. }
  104. }
  105. if (selectData.notes.find((item:any) => item.id === noteItem.id)) {
  106. //
  107. } else {
  108. selectData.notes.push(noteItem);
  109. }
  110. // selectData.notes.push(noteItem);
  111. notesList.push(item.noteId);
  112. }
  113. }
  114. // 如果小节里面有拍号,需要减去拍号左侧的宽度
  115. let beatWidth = '100%';
  116. let beatLeft = 0;
  117. if (!MeasureNumberXMLList.includes(item.MeasureNumberXML)) {
  118. if (item.stave) {
  119. if (item.stave?.attrs?.id) {
  120. const staveEle = document.querySelector(`#${item.stave.attrs.id}`);
  121. const list = [
  122. Array.from(staveEle?.querySelectorAll(".vf-clef") || []),
  123. Array.from(staveEle?.querySelectorAll(".vf-keysignature") || []),
  124. Array.from(staveEle?.getElementsByTagName("text") || []),
  125. ].flat();
  126. try {
  127. if (list.length) {
  128. // console.log("🚀 ~ list:", list)
  129. list.forEach((_el: any) => {
  130. _el?.style?.setProperty("display", "none");
  131. });
  132. }
  133. } catch (error) {}
  134. const staveBbox = staveEle?.getBoundingClientRect?.() || { x: 0, width: 0, y: 0, height: 0 };
  135. const timesignatureDom = staveEle?.querySelector('.vf-timesignature')
  136. if (timesignatureDom) {
  137. const timesignatureBbox = timesignatureDom.getBoundingClientRect()
  138. const leftWidth = timesignatureBbox.x + timesignatureBbox.width - staveBbox.x
  139. beatLeft = leftWidth
  140. beatWidth = `calc(100% - ${leftWidth+'px'})`
  141. }
  142. if (i === 0) {
  143. minMeasureHeigt = staveBbox.height
  144. }
  145. try {
  146. if (list.length) {
  147. list.forEach((_el: any) => {
  148. _el?.style?.removeProperty("display");
  149. });
  150. }
  151. } catch (error) {}
  152. // console.log("🚀 ~ staveEle:", staveBbox.height)
  153. selectData.measureHeight = staveBbox.height
  154. let compareVal = staveBbox.height - minMeasureHeigt
  155. compareVal = compareVal > 0 ? compareVal : 0
  156. selectData.measureHeight = staveBbox.height - compareVal
  157. noteItem.staveBox = {
  158. left: staveBbox.x - parentLeft + "px",
  159. // top: ((item.stave.y || 0) - 5) * state.zoom + "px",
  160. top: staveBbox.y - parentTop + compareVal + "px",
  161. width: staveBbox.width + "px",
  162. height: staveBbox.height - compareVal + "px",
  163. // background: 'rgba(0,0,0,.2)'
  164. };
  165. selectData.staves.push(noteItem);
  166. }
  167. MeasureNumberXMLList.push(item.MeasureNumberXML);
  168. beatMeasureWidths[item.MeasureNumberXML] = {
  169. beatLeft,
  170. beatWidth
  171. }
  172. } else {
  173. if (item.multipleRestMeasures) {
  174. if (state.isCombineRender) {
  175. let currentItem = null;
  176. for (let index = 0; index < state.vfmeasures.length; index++) {
  177. const element = state.vfmeasures[index];
  178. const measureNum = element.getAttribute('data-num') ? Number(element.getAttribute('data-num')) : -1;
  179. const nextMeasureNum = state.vfmeasures[index+1]?.getAttribute('data-num') ? Number(state.vfmeasures[index+1]?.getAttribute('data-num')) : -1;
  180. if (measureNum === item.MeasureNumberXML || item.MeasureNumberXML < nextMeasureNum || nextMeasureNum == -1) {
  181. currentItem = element
  182. break;
  183. }
  184. }
  185. const staveBbox = currentItem?.querySelector('.vf-stave')?.getBoundingClientRect() || { x: 0, width: 0, y: 0, height: 0 };
  186. if (currentItem) {
  187. noteItem.staveBox = {
  188. left: staveBbox.x - parentLeft + "px",
  189. // top: ((item.stave.y || 0) - 5) * state.zoom + "px",
  190. top: staveBbox.y - parentTop + "px",
  191. width: staveBbox.width + "px",
  192. height: staveBbox.height + "px",
  193. // height: preItem.staveBox.height,
  194. };
  195. selectData.staves.push(noteItem);
  196. MeasureNumberXMLList.push(item.MeasureNumberXML);
  197. beatMeasureWidths[item.MeasureNumberXML] = {
  198. beatLeft,
  199. beatWidth
  200. }
  201. }
  202. } else {
  203. const preItem = selectData.staves.find(
  204. (n: any) => n.MeasureNumberXML === item.MeasureNumberXML - 1
  205. );
  206. if (preItem?.staveBox) {
  207. noteItem.staveBox = {
  208. left: preItem.staveBox.left,
  209. top: preItem.staveBox.top,
  210. width: preItem.staveBox.width,
  211. // height: preItem.staveBox.height,
  212. };
  213. selectData.staves.push(noteItem);
  214. MeasureNumberXMLList.push(item.MeasureNumberXML);
  215. beatMeasureWidths[item.MeasureNumberXML] = {
  216. beatLeft,
  217. beatWidth
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. // 部分浏览器渲染的第一小节的位置信息会包含拍号、调号,需要处理一下,剔除掉拍号、调号的位置
  226. if (selectData.staves[0]?.staveBox?.top !== selectData.staves[1]?.staveBox?.top) {
  227. selectData.staves[0].staveBox.top = selectData.staves[1]?.staveBox?.top || selectData.staves[0]?.staveBox?.top
  228. }
  229. console.log("🚀 ~ selectData.notes:", selectData.notes, selectData.staves,beatMeasureWidths,MeasureNumberXMLList);
  230. };
  231. /** 是否可以点击音符 */
  232. const disableClickNote = computed(() => {
  233. return (state.sectionStatus && state.section.length != 2) || (state.modeType === "evaluating");
  234. });
  235. // 选段符号
  236. const sectionPosData = computed(() => {
  237. if(state.sectionStatus) {
  238. return state.section.map(((item,index) => {
  239. if(index === 0){
  240. const currItem = selectData.staves.find(stave => {
  241. return stave.MeasureNumberXML === item.MeasureNumberXML
  242. })
  243. // 获取stave里面vf-custom-bg的位置坐标,才是准确的坐标
  244. // const currBgX = document.getElementById(currItem.stave.attrs.id)?.querySelector('.vf-custom-bg')?.getBoundingClientRect()?.x || 0;
  245. const currBgX = currItem.stave?.attrs && currItem.stave.attrs.id ? document.getElementById(currItem.stave.attrs.id)?.querySelector('.vf-custom-bg')?.getBBox()?.x * state.zoom || 0 : 0;
  246. return currItem && {
  247. left: currBgX ? currBgX + 'px' : currItem.staveBox.left,
  248. top: currItem.staveBox.top,
  249. height: selectData.measureHeight + 'px' // 小节的高度
  250. }
  251. } else {
  252. // 实际的结束位置
  253. const actualEndIndex = state.userChooseEndIndex > item.MeasureNumberXML ? state.userChooseEndIndex : item.MeasureNumberXML
  254. const currItem = selectData.staves.find(stave => {
  255. return stave.MeasureNumberXML === actualEndIndex
  256. })
  257. return currItem && {
  258. left: parseFloat(currItem.staveBox.left)+parseFloat(currItem.staveBox.width)-2 +"px",
  259. top: currItem.staveBox.top,
  260. height: selectData.measureHeight + 'px'
  261. }
  262. }
  263. }))
  264. }
  265. return []
  266. })
  267. onMounted(() => {
  268. selectData.notes = [];
  269. selectData.staves = [];
  270. calcNoteData();
  271. const img: HTMLElement = document.querySelector('#cursorImg-0')!
  272. if (metronomeData.cursorMode === 2){
  273. img.classList.add('lineHide')
  274. } else {
  275. img.classList.remove('lineHide')
  276. }
  277. // 初始化谱面可移动的元素位置
  278. try {
  279. moveData.partIndex = state.partIndex + ""
  280. // 速度标记元素和谱面并非同时渲染,初始化可移动元素的时候,需要加个延迟
  281. setTimeout(() => {
  282. renderForMoveData()
  283. }, 0);
  284. } catch (error) {}
  285. });
  286. return () => (
  287. <>
  288. <div
  289. id="selectionBox"
  290. class={[
  291. styles.selectionContainer,
  292. isPad && styles.isPad,
  293. state.zoom == 1.25 ? styles.middleZoom : state.zoom == 1.5 ? styles.bigZoom : state.zoom == 1.75 ? styles.largeZoom : state.zoom == 2 ? styles.largeZoom2 : state.zoom == 2.25 ? styles.largeZoom3 :
  294. state.zoom == 0.65 ? styles.smallZoom : state.zoom == 0.5 ? styles.litteZoom : ''
  295. ]}
  296. onClick={(e: Event) => e.stopPropagation()}
  297. >
  298. {selectData.staves.map((item: any, index) => {
  299. // 评测得分
  300. const scoreItem = item.id && evaluatingData.evaluatings[item.measureListIndex];
  301. // for(let idx in evaluatingData.evaluatings) {
  302. // const { show, measureIndex } = evaluatingData.evaluatings[idx]
  303. // if (show && measureIndex !== item.measureListIndex) {
  304. // evaluatingData.evaluatings[idx].show = false
  305. // }
  306. // }
  307. // 高级模式下,显示节拍线
  308. // 不是报告模式
  309. // 不是多小节休止符
  310. // 节拍线开关
  311. // 当前小节
  312. // 当前小节
  313. /* 节拍指针,现在没有节拍器指针了,但是以后要加上的话,这里需要性能优化。现在这样每次节拍指针更新都会刷新这里的虚拟dom */
  314. const lineShow =
  315. !state.isReport &&
  316. metronomeData.cursorMode === 2 &&
  317. item.MeasureNumberXML === metronomeData.activeMetro?.measureNumberXML &&
  318. state.times[state.activeNoteIndex].MeasureNumberXML === item.MeasureNumberXML;
  319. //console.log('显示节拍指针',lineShow,state.times[state.activeNoteIndex].MeasureNumberXML,item.MeasureNumberXML,metronomeData.activeMetro?.measureNumberXML)
  320. return (
  321. <>
  322. {item.staveBox && (
  323. <div
  324. key={item.id}
  325. class={[
  326. styles.position,
  327. // scoreItem ? `scoreItemLeve${scoreItem.leve}` : "", // 去掉评测小节得分的背景色
  328. (state.platform === IPlatform.PC && state.zoom > 0.8) ? styles.linePC : '',
  329. `measureIndex_${item.MeasureNumberXML}`
  330. ]}
  331. style={item.staveBox}
  332. onClick={() => {
  333. // 当为连续休止小节的结束选段的时候 应该传休止小节 结束的位置
  334. let staveItem = item
  335. if(state.section.length === 1 && item.totalMultipleRestMeasures > 0){
  336. staveItem = selectData.staves[index + item.totalMultipleRestMeasures - 1]
  337. }
  338. handleSelection(staveItem)
  339. }}
  340. >
  341. {lineShow && (
  342. <div style={{height: selectData.measureHeight + 'px', position: 'relative', width: beatMeasureWidths[item.MeasureNumberXML].beatWidth, left: beatMeasureWidths[item.MeasureNumberXML].beatLeft + 'px'}}>
  343. <div
  344. class={[
  345. styles.line,
  346. state.setting.eyeProtection ? styles.eyeLine : '',
  347. state.musicRenderType == EnumMusicRenderType.staff ? styles.lineStaff : styles.lineJianPu,
  348. ]}
  349. style={{ left: metronomeData.activeMetro.left }}></div>
  350. </div>
  351. )}
  352. {!state.isReport &&
  353. !!item.multipleRestMeasures &&
  354. <MultipleRestMeasures item = {item}></MultipleRestMeasures>
  355. }
  356. <Transition
  357. name="centerTop"
  358. onAfterEnter={() => {
  359. scoreItem.show = false;
  360. }}
  361. >
  362. {scoreItem?.show && (
  363. <div
  364. class={styles.scoreItem}
  365. style={{ color: leveByScoreMeasureIcons[scoreItem.leve]?.color || "" }}
  366. >
  367. <img src={leveByScoreMeasureIcons[scoreItem.leve]?.icon} />
  368. <span>{scoreItem.score}</span>
  369. </div>
  370. )}
  371. </Transition>
  372. </div>
  373. )}
  374. </>
  375. );
  376. })}
  377. {selectData.notes.map((item: any) => {
  378. return (
  379. <div
  380. class={[styles.position, disableClickNote.value && styles.disable, styles.note, `noteIndex_${item.index}`]}
  381. style={item.bbox}
  382. onClick={() => skipNotePlay(item.index, false, 'manual')}
  383. >
  384. {/* <div class={styles.noteFollow} data-vf={"vf" + item.id}>
  385. <Icon name="success" />
  386. <Icon name="cross" />
  387. </div> */}
  388. <div class={styles.noteFollow} data-vf={"vf" + item.id}>
  389. {/* <Icon name="success" />
  390. <Icon name="cross" /> */}
  391. <div class={[styles.followTipUp, 'tip-up']}>
  392. <img src={IntonationUp} />
  393. {/* <span>音准<i>高了</i></span> */}
  394. </div>
  395. <div class={[styles.followTipDown, 'tip-down']}>
  396. <img src={IntonationDown} />
  397. {/* <span>音准<i>低了</i></span> */}
  398. </div>
  399. </div>
  400. <div class={[styles.noteDot, 'node-dot']}></div>
  401. </div>
  402. );
  403. })}
  404. {/* 选段 */}
  405. {
  406. sectionPosData.value.map((item,index) =>{
  407. return (
  408. item && <div class={styles.selectBox} style={item}>
  409. <div class={[styles.selectHandle,index>0&&styles.selectHandleRight,(state.playState==="play" || state.isHomeWork)&&styles.playIng]} onClick={()=>{
  410. // 如果选择了2个 删除左边的时候
  411. if (state.section.length===1&&index === 0) {
  412. // #bug:11552
  413. resetBaseRate(state.activeNoteIndex);
  414. }
  415. if(state.section.length===2&&index === 0){
  416. state.section = []
  417. // 重置速度和播放倍率
  418. resetBaseRate(state.activeNoteIndex);
  419. showToast({
  420. message: "请选择开始小节",
  421. duration: 0,
  422. position: "top",
  423. className: "selectionToast",
  424. });
  425. }else{
  426. state.section.splice(index,1)
  427. state.section = [...state.section] // 触发 watch
  428. showToast({
  429. message: state.section.length?"请选择结束小节":"请选择开始小节",
  430. duration: 0,
  431. position: "top",
  432. className: "selectionToast",
  433. });
  434. }
  435. // IOS18.1.1浏览器渲染更新有问题,需要手动更新一下
  436. const selectionDom = document.getElementById('selectionBox')
  437. if (selectionDom) {
  438. selectionDom.style.display = 'none';
  439. requestAnimationFrame(() => {
  440. selectionDom.style.display = 'block';
  441. })
  442. }
  443. }}></div>
  444. </div>
  445. )
  446. })
  447. }
  448. {/* 移动模块 */}
  449. {query.isMove == "1" && <MoveMusicScore />}
  450. </div>
  451. </>
  452. );
  453. },
  454. });