index.tsx 15 KB

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