CoursewarePreview.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. <template>
  2. <div ref="courserware" class="courserware" :style="computedCourserwareStyle()" @mouseup="handleTextSelection">
  3. <template v-for="(row, i) in data.row_list">
  4. <div v-show="computedRowVisibility(row.row_id)" :key="i" class="row" :style="getMultipleColStyle(i)">
  5. <el-checkbox
  6. v-if="
  7. isShowGroup &&
  8. groupRowList.find(({ row_id, is_pre_same_group }) => row.row_id === row_id && !is_pre_same_group)
  9. "
  10. v-model="rowCheckList[row.row_id]"
  11. :class="['row-checkbox', `${row.row_id}`]"
  12. />
  13. <!-- 列 -->
  14. <template v-for="(col, j) in row.col_list">
  15. <div :key="j" :class="['col', `col-${i}-${j}`]" :style="computedColStyle(col)">
  16. <!-- 网格 -->
  17. <template v-for="(grid, k) in col.grid_list">
  18. <component
  19. :is="previewComponentList[grid.type]"
  20. :id="grid.id"
  21. :key="k"
  22. ref="preview"
  23. :content="computedColContent(grid.id)"
  24. :class="[grid.id]"
  25. :data-id="grid.id"
  26. :style="{
  27. gridArea: grid.grid_area,
  28. height: grid.height,
  29. }"
  30. @contextmenu.native.prevent="handleContextMenu($event, grid.id)"
  31. @handleHeightChange="handleHeightChange"
  32. />
  33. <div
  34. v-if="showMenu && componentId === grid.id"
  35. :key="'menu' + grid.id + k"
  36. class="custom-context-menu"
  37. :style="{ left: menuPosition.x + 'px', top: menuPosition.y + 'px' }"
  38. @click="handleMenuItemClick"
  39. >
  40. 添加批注
  41. </div>
  42. <div
  43. v-if="showRemark && Object.keys(componentRemarkObj).length !== 0 && componentRemarkObj[grid.id]"
  44. :key="'show' + grid.id + k"
  45. >
  46. <el-popover
  47. v-for="(items, indexs) in componentRemarkObj[grid.id]"
  48. :key="indexs"
  49. placement="bottom"
  50. width="200"
  51. trigger="click"
  52. >
  53. <div v-html="items.content"></div>
  54. <template #reference>
  55. <SvgIcon
  56. slot="reference"
  57. icon-class="icon-info"
  58. size="24"
  59. class="remark-info"
  60. :style="{ left: items.position_x - 12 + 'px', top: items.position_y - 12 + 'px' }"
  61. />
  62. </template>
  63. </el-popover>
  64. </div>
  65. </template>
  66. </div>
  67. </template>
  68. </div>
  69. </template>
  70. <!-- 选中文本的工具栏 -->
  71. <div v-show="showToolbar" class="contentmenu" :style="contentmenu">
  72. <span class="button" @click="setNote"><SvgIcon icon-class="sidebar-text" size="14" /> 笔记</span>
  73. <span class="line"></span>
  74. <span class="button" @click="setCollect"><SvgIcon icon-class="sidebar-collect" size="14" /> 收藏 </span>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. import { previewComponentList } from '@/views/book/courseware/data/bookType';
  80. export default {
  81. name: 'CoursewarePreview',
  82. provide() {
  83. return {
  84. getDragStatus: () => false,
  85. bookInfo: this.bookInfo,
  86. };
  87. },
  88. props: {
  89. data: {
  90. type: Object,
  91. default: () => ({}),
  92. },
  93. coursewareId: {
  94. type: String,
  95. default: '',
  96. },
  97. background: {
  98. type: Object,
  99. default: () => ({}),
  100. },
  101. componentList: {
  102. type: Array,
  103. required: true,
  104. },
  105. canRemark: {
  106. type: Boolean,
  107. default: false,
  108. },
  109. showRemark: {
  110. type: Boolean,
  111. default: false,
  112. },
  113. componentRemarkObj: {
  114. type: Object,
  115. default: () => ({}),
  116. },
  117. groupRowList: {
  118. type: Array,
  119. default: () => [],
  120. },
  121. isShowGroup: {
  122. type: Boolean,
  123. default: false,
  124. },
  125. groupShowAll: {
  126. type: Boolean,
  127. default: true,
  128. },
  129. project: {
  130. type: Object,
  131. default: () => ({}),
  132. },
  133. },
  134. data() {
  135. return {
  136. previewComponentList,
  137. courseware_id: this.coursewareId,
  138. bookInfo: {
  139. theme_color: '',
  140. },
  141. showMenu: false,
  142. divPosition: {
  143. left: 0,
  144. top: 0,
  145. }, // courserware盒子原始距离页面顶部和左边的距离
  146. menuPosition: { x: 0, y: 0, select_node: '' }, // 用于存储菜单的位置
  147. componentId: '', // 添加批注的组件id
  148. rowCheckList: {},
  149. showToolbar: false,
  150. contentmenu: {
  151. left: 0,
  152. top: 0,
  153. },
  154. selectedInfo: null,
  155. };
  156. },
  157. watch: {
  158. groupRowList: {
  159. handler(val) {
  160. if (!val) return;
  161. this.rowCheckList = val
  162. .filter(({ is_pre_same_group }) => !is_pre_same_group)
  163. .reduce((acc, row) => {
  164. acc[row.row_id] = false;
  165. return acc;
  166. }, {});
  167. },
  168. },
  169. coursewareId: {
  170. handler(val) {
  171. this.courseware_id = val;
  172. },
  173. },
  174. },
  175. mounted() {
  176. const element = this.$refs.courserware;
  177. const rect = element.getBoundingClientRect();
  178. this.divPosition = {
  179. left: rect.left,
  180. top: rect.top,
  181. };
  182. window.addEventListener('mousedown', this.handleMouseDown);
  183. },
  184. beforeDestroy() {
  185. window.removeEventListener('mousedown', this.handleMouseDown);
  186. },
  187. methods: {
  188. /**
  189. * 计算组件内容
  190. * @param {string} id 组件id
  191. * @returns {string} 组件内容
  192. */
  193. computedColContent(id) {
  194. if (!id) return '';
  195. return this.componentList.find((item) => item.component_id === id)?.content || '';
  196. },
  197. getMultipleColStyle(i) {
  198. let row = this.data.row_list[i];
  199. let col = row.col_list;
  200. if (col.length <= 1) {
  201. return {
  202. gridTemplateColumns: '100fr',
  203. };
  204. }
  205. let gridTemplateColumns = row.width_list.join(' ');
  206. return {
  207. gridAutoFlow: 'column',
  208. gridTemplateColumns,
  209. gridTemplateRows: 'auto',
  210. };
  211. },
  212. /**
  213. * 计算行的可见性
  214. * @params {string} rowId 行的ID
  215. */
  216. computedRowVisibility(rowId) {
  217. if (this.groupShowAll) return true;
  218. let { row_id, is_pre_same_group } = this.groupRowList.find(({ row_id }) => row_id === rowId);
  219. if (is_pre_same_group) {
  220. const index = this.groupRowList.findIndex(({ row_id }) => row_id === rowId);
  221. if (index === -1) return false;
  222. for (let i = index - 1; i >= 0; i--) {
  223. if (!this.groupRowList[i].is_pre_same_group) {
  224. return this.rowCheckList[this.groupRowList[i].row_id];
  225. }
  226. }
  227. return false;
  228. }
  229. return this.rowCheckList[row_id];
  230. },
  231. /**
  232. * 分割整数为多个 1的倍数
  233. * @param {number} num
  234. * @param {number} parts
  235. */
  236. splitInteger(num, parts) {
  237. let base = Math.floor(num / parts);
  238. let arr = Array(parts).fill(base);
  239. let remainder = num - base * parts;
  240. for (let i = 0; remainder > 0; i = (i + 1) % parts) {
  241. arr[i] += 1;
  242. remainder -= 1;
  243. }
  244. return arr;
  245. },
  246. computedColStyle(col) {
  247. const grid = col.grid_list;
  248. let maxCol = 0; // 最大列数
  249. let rowList = new Map();
  250. grid.forEach(({ row }) => {
  251. rowList.set(row, (rowList.get(row) || 0) + 1);
  252. });
  253. let curMaxRow = 0; // 当前数量最大 row 的值
  254. rowList.forEach((value, key) => {
  255. if (value > maxCol) {
  256. maxCol = value;
  257. curMaxRow = key;
  258. }
  259. });
  260. // 计算 grid_template_areas
  261. let gridTemplateAreas = '';
  262. let gridArr = [];
  263. grid.forEach(({ grid_area, row }) => {
  264. if (!gridArr[row - 1]) {
  265. gridArr[row - 1] = [];
  266. }
  267. if (curMaxRow === row) {
  268. gridArr[row - 1].push(`${grid_area}`);
  269. } else {
  270. let filter = grid.filter((item) => item.row === row);
  271. let find = filter.findIndex((item) => item.grid_area === grid_area);
  272. let needNum = maxCol - filter.length; // 需要的数量
  273. let str = '';
  274. if (filter.length === 1) {
  275. str = ` ${grid_area} `.repeat(needNum + 1);
  276. } else {
  277. let arr = this.splitInteger(needNum, filter.length);
  278. str = arr[find] === 0 ? ` ${grid_area} ` : ` ${grid_area} `.repeat(arr[find] + 1);
  279. }
  280. gridArr[row - 1].push(`${str}`);
  281. }
  282. });
  283. gridArr.forEach((item) => {
  284. gridTemplateAreas += `'${item.join(' ')}' `;
  285. });
  286. // 计算 grid_template_columns
  287. let gridTemplateColumns = '';
  288. let max = { row: 0, num: 0 };
  289. grid.forEach(({ row }) => {
  290. // 计算出 row 的哪个值最多
  291. let len = grid.filter((item) => item.row === row).length;
  292. if (max.num < len) {
  293. max.num = len;
  294. max.row = row;
  295. }
  296. });
  297. grid.forEach((item) => {
  298. if (item.row === max.row) {
  299. gridTemplateColumns += `${item.width} `;
  300. }
  301. });
  302. // 计算 grid_template_rows
  303. let gridTemplateRows = '';
  304. // 将 grid 按照 row 分组
  305. let gridMap = new Map();
  306. grid.forEach((item) => {
  307. if (!gridMap.has(item.row)) {
  308. gridMap.set(item.row, []);
  309. }
  310. gridMap.get(item.row).push(item.height);
  311. });
  312. gridMap.forEach((value) => {
  313. if (value.length === 1) {
  314. gridTemplateRows += `${value[0]} `;
  315. } else {
  316. let isAllAuto = value.every((item) => item === 'auto'); // 是否全是 auto
  317. gridTemplateRows += isAllAuto ? 'auto ' : `max(${value.join(', ')}) `;
  318. }
  319. });
  320. return {
  321. width: col.width,
  322. gridTemplateAreas,
  323. gridTemplateColumns,
  324. gridTemplateRows,
  325. };
  326. },
  327. /**
  328. * 计算课件背景样式
  329. * @returns {Object} 课件背景样式对象
  330. */
  331. computedCourserwareStyle() {
  332. const { background_image_url: bcImgUrl = '', background_position: pos = {} } = this.background || {};
  333. const hasNoRows = !Array.isArray(this.data?.row_list) || this.data.row_list.length === 0;
  334. const projectCover = this.project?.cover_image_file_url || '';
  335. // 优先在空行时使用背景图或项目封面
  336. const backgroundImage = hasNoRows ? bcImgUrl || projectCover : '';
  337. // 保护性读取位置/大小值,避免 undefined 导致字符串 "undefined%"
  338. const widthPct = typeof pos.width === 'undefined' ? '' : pos.width;
  339. const heightPct = typeof pos.height === 'undefined' ? '' : pos.height;
  340. const leftPct = typeof pos.left === 'undefined' ? '' : pos.left;
  341. const topPct = typeof pos.top === 'undefined' ? '' : pos.top;
  342. const hasBcImg = Boolean(bcImgUrl);
  343. const backgroundSize = hasBcImg ? `${widthPct}% ${heightPct}%` : hasNoRows ? 'contain' : '';
  344. const backgroundPosition = hasBcImg ? `${leftPct}% ${topPct}%` : hasNoRows ? 'center' : '';
  345. return {
  346. backgroundImage: backgroundImage ? `url(${backgroundImage})` : '',
  347. backgroundSize,
  348. backgroundPosition,
  349. };
  350. },
  351. handleContextMenu(event, id) {
  352. if (this.canRemark) {
  353. event.preventDefault(); // 阻止默认的上下文菜单显示
  354. this.menuPosition = {
  355. x: event.clientX - this.divPosition.left,
  356. y: event.clientY - this.divPosition.top,
  357. }; // 设置菜单位置
  358. this.componentId = id;
  359. this.$emit('computeScroll');
  360. }
  361. },
  362. handleResult(top, left, select_node) {
  363. this.menuPosition = {
  364. x: this.menuPosition.x + left,
  365. y: this.menuPosition.y + top,
  366. select_node,
  367. }; // 设置菜单位置
  368. this.showMenu = true; // 显示菜单
  369. },
  370. handleMenuItemClick() {
  371. this.showMenu = false; // 隐藏菜单
  372. this.$emit(
  373. 'addRemark',
  374. this.menuPosition.select_node,
  375. this.menuPosition.x,
  376. this.menuPosition.y,
  377. this.componentId,
  378. );
  379. },
  380. handleMouseDown(event) {
  381. if (event.button === 0 && event.target.className !== 'custom-context-menu') {
  382. // 0 表示左键
  383. this.showMenu = false;
  384. }
  385. },
  386. /**
  387. * 查找子组件
  388. * @param {string} id 组件的唯一标识符
  389. * @returns {Promise<HTMLElement|null>} 返回找到的子组件或 null
  390. */
  391. async findChildComponentByKey(id) {
  392. await this.$nextTick();
  393. return this.$refs.preview.find((child) => child.$el && child.$el.dataset && child.$el.dataset.id === id);
  394. },
  395. /**
  396. * 模拟回答
  397. * @param {boolean} isJudgingRightWrong 是否判断对错
  398. * @param {boolean} isShowRightAnswer 是否显示正确答案
  399. * @param {boolean} disabled 是否禁用
  400. */
  401. simulateAnswer(isJudgingRightWrong, isShowRightAnswer, disabled = true) {
  402. this.$refs.preview.forEach((item) => {
  403. item.showAnswer(isJudgingRightWrong, isShowRightAnswer, null, disabled);
  404. });
  405. },
  406. /**
  407. * 处理组件高度变化事件
  408. * @param {string} id 组件id
  409. * @param {string} newHeight 组件的新高度
  410. */
  411. handleHeightChange(id, newHeight) {
  412. this.data.row_list.forEach((row) => {
  413. row.col_list.forEach((col) => {
  414. col.grid_list.forEach((grid) => {
  415. if (grid.id === id) {
  416. grid.height = newHeight;
  417. }
  418. });
  419. });
  420. });
  421. },
  422. // 处理选中文本
  423. handleTextSelection() {
  424. this.showToolbar = false;
  425. // 延迟处理,确保选择已完成
  426. setTimeout(() => {
  427. const selection = window.getSelection();
  428. if (selection.toString().trim() === '') return null;
  429. const selectedText = selection.toString().trim();
  430. const range = selection.getRangeAt(0);
  431. console.info(selectedText, range);
  432. this.showToolbar = true;
  433. const container = document.querySelector('.courserware');
  434. const boxRect = container.getBoundingClientRect();
  435. const selectRect = range.getBoundingClientRect();
  436. this.contentmenu = {
  437. left: `${Math.round(selectRect.left - boxRect.left + selectRect.width / 2 - 63)}px`,
  438. top: `${Math.round(selectRect.top - boxRect.top + selectRect.height)}px`, // 向上偏移10px
  439. };
  440. this.selectedInfo = {
  441. text: selectedText,
  442. range,
  443. };
  444. }, 100);
  445. },
  446. // 笔记
  447. setNote() {
  448. this.showToolbar = false;
  449. this.oldRichData = {};
  450. let info = this.getSelectionInfo();
  451. if (!info) return;
  452. info.coursewareId = this.courseware_id;
  453. this.$emit('editNote', info);
  454. this.selectedInfo = null;
  455. },
  456. // 加入收藏
  457. setCollect() {
  458. this.showToolbar = false;
  459. let info = this.getSelectionInfo();
  460. if (!info) return;
  461. info.coursewareId = this.courseware_id;
  462. this.$emit('saveCollect', info);
  463. this.selectedInfo = null;
  464. },
  465. // 定位
  466. handLocation(item) {
  467. this.scrollToDataId(item.blockId);
  468. },
  469. getSelectionInfo() {
  470. if (!this.selectedInfo) return;
  471. const range = this.selectedInfo.range;
  472. let selectedText = this.selectedInfo.text;
  473. if (!selectedText) return null;
  474. let commonAncestor = range.commonAncestorContainer;
  475. if (commonAncestor.nodeType === Node.TEXT_NODE) {
  476. commonAncestor = commonAncestor.parentNode;
  477. }
  478. const blockElement = commonAncestor.closest('[data-id]');
  479. if (!blockElement) return null;
  480. const blockId = blockElement.dataset.id;
  481. // 获取所有汉字元素
  482. const charElements = blockElement.querySelectorAll('.py-char,.rich-text,.NNPE-chs');
  483. // 构建包含位置信息的文本数组
  484. const textFragments = Array.from(charElements)
  485. .map((el, index) => {
  486. let text = '';
  487. if (el.classList.contains('rich-text')) {
  488. const pElements = Array.from(el.querySelectorAll('p'));
  489. text = pElements.map((p) => p.textContent.trim()).join('');
  490. } else if (el.classList.contains('NNPE-chs')) {
  491. const spanElements = Array.from(el.querySelectorAll('span'));
  492. spanElements.push(el);
  493. text = spanElements.map((span) => span.textContent.trim()).join('');
  494. } else {
  495. text = el.textContent.trim();
  496. }
  497. // 过滤掉拼音和空文本
  498. if (!text || /^[a-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜü]+$/i.test(text)) {
  499. return { text: '', element: el, index };
  500. }
  501. return { text, element: el, index };
  502. })
  503. .filter((fragment) => fragment.text);
  504. // 获取完整的纯文本
  505. const fullText = textFragments.map((f) => f.text).join('');
  506. // 清理选中文本
  507. let cleanSelectedText = selectedText.replace(/\n/g, '').trim();
  508. cleanSelectedText = cleanSelectedText.replace(/[a-zāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜü]/gi, '').trim();
  509. if (!cleanSelectedText) return null;
  510. // 方案1A:使用Range的边界点精确定位
  511. try {
  512. const startContainer = range.startContainer;
  513. const endContainer = range.endContainer;
  514. const startOffset = range.startOffset;
  515. const endOffset = range.endOffset;
  516. // 找到选择开始的元素在textFragments中的位置
  517. let startFragmentIndex = -1;
  518. let cumulativeLength = 0;
  519. let startIndexInFullText = -1;
  520. for (let i = 0; i < textFragments.length; i++) {
  521. const fragment = textFragments[i];
  522. // 检查这个元素是否包含选择起点
  523. if (fragment.element.contains(startContainer) || fragment.element === startContainer) {
  524. // 计算在这个元素内的起始位置
  525. if (startContainer.nodeType === Node.TEXT_NODE) {
  526. // 如果是文本节点,需要计算在父元素中的偏移
  527. const elementText = fragment.text;
  528. startFragmentIndex = i;
  529. startIndexInFullText = cumulativeLength + Math.min(startOffset, elementText.length);
  530. break;
  531. } else {
  532. // 如果是元素节点,从0开始
  533. startFragmentIndex = i;
  534. startIndexInFullText = cumulativeLength;
  535. break;
  536. }
  537. }
  538. cumulativeLength += fragment.text.length;
  539. }
  540. if (startIndexInFullText === -1) {
  541. // 如果精确定位失败,回退到文本匹配(但使用更智能的匹配)
  542. return this.fallbackToTextMatch(fullText, cleanSelectedText, range, textFragments);
  543. }
  544. const endIndexInFullText = startIndexInFullText + cleanSelectedText.length;
  545. return {
  546. blockId,
  547. text: cleanSelectedText,
  548. startIndex: startIndexInFullText,
  549. endIndex: endIndexInFullText,
  550. fullText,
  551. };
  552. } catch (error) {
  553. console.warn('精确位置计算失败,使用备选方案:', error);
  554. return this.fallbackToTextMatch(fullText, cleanSelectedText, range, textFragments);
  555. }
  556. },
  557. // 备选方案:基于DOM位置的智能匹配
  558. fallbackToTextMatch(fullText, selectedText, range, textFragments) {
  559. // 获取选择范围的近似位置
  560. const rangeRect = range.getBoundingClientRect();
  561. // 找到最接近选择中心的文本片段
  562. let closestFragment = null;
  563. let minDistance = Infinity;
  564. textFragments.forEach((fragment) => {
  565. const rect = fragment.element.getBoundingClientRect();
  566. if (rect.width > 0 && rect.height > 0) {
  567. // 确保元素可见
  568. const centerX = rect.left + rect.width / 2;
  569. const centerY = rect.top + rect.height / 2;
  570. const rangeCenterX = rangeRect.left + rangeRect.width / 2;
  571. const rangeCenterY = rangeRect.top + rangeRect.height / 2;
  572. const distance = Math.sqrt(Math.pow(centerX - rangeCenterX, 2) + Math.pow(centerY - rangeCenterY, 2));
  573. if (distance < minDistance) {
  574. minDistance = distance;
  575. closestFragment = fragment;
  576. }
  577. }
  578. });
  579. if (closestFragment) {
  580. // 从最近的片段开始向前后搜索匹配
  581. const fragmentIndex = textFragments.indexOf(closestFragment);
  582. let cumulativeLength = 0;
  583. // 计算到当前片段的累计长度
  584. for (let i = 0; i < fragmentIndex; i++) {
  585. cumulativeLength += textFragments[i].text.length;
  586. }
  587. // 在当前片段附近搜索匹配
  588. const searchStart = Math.max(0, cumulativeLength - selectedText.length * 3);
  589. const searchEnd = Math.min(
  590. fullText.length,
  591. cumulativeLength + closestFragment.text.length + selectedText.length * 3,
  592. );
  593. const searchArea = fullText.substring(searchStart, searchEnd);
  594. const localIndex = searchArea.indexOf(selectedText);
  595. if (localIndex !== -1) {
  596. return {
  597. startIndex: searchStart + localIndex,
  598. endIndex: searchStart + localIndex + selectedText.length,
  599. text: selectedText,
  600. fullText,
  601. };
  602. }
  603. }
  604. // 最终回退:使用所有匹配位置,选择最合理的一个
  605. const allMatches = [];
  606. let searchIndex = 0;
  607. while ((searchIndex = fullText.indexOf(selectedText, searchIndex)) !== -1) {
  608. allMatches.push(searchIndex);
  609. searchIndex += selectedText.length;
  610. }
  611. if (allMatches.length === 1) {
  612. return {
  613. startIndex: allMatches[0],
  614. endIndex: allMatches[0] + selectedText.length,
  615. text: selectedText,
  616. fullText,
  617. };
  618. } else if (allMatches.length > 1) {
  619. // 如果有多个匹配,选择位置最接近选择中心的
  620. if (closestFragment) {
  621. let cumulativeLength = 0;
  622. let fragmentStartIndex = 0;
  623. for (let i = 0; i < textFragments.length; i++) {
  624. if (textFragments[i] === closestFragment) {
  625. fragmentStartIndex = cumulativeLength;
  626. break;
  627. }
  628. cumulativeLength += textFragments[i].text.length;
  629. }
  630. // 选择最接近当前片段起始位置的匹配
  631. const bestMatch = allMatches.reduce((best, current) => {
  632. return Math.abs(current - fragmentStartIndex) < Math.abs(best - fragmentStartIndex) ? current : best;
  633. });
  634. return {
  635. startIndex: bestMatch,
  636. endIndex: bestMatch + selectedText.length,
  637. text: selectedText,
  638. fullText,
  639. };
  640. }
  641. }
  642. return null;
  643. },
  644. scrollToDataId(dataId, offset) {
  645. if (!offset) offset = 0;
  646. const element = document.querySelector(`div[data-id="${dataId}"]`);
  647. if (element) {
  648. const elementPosition = element.getBoundingClientRect().top + window.pageYOffset;
  649. const offsetPosition = elementPosition - offset;
  650. element.scrollIntoView({
  651. behavior: 'smooth', // 滚动行为:'auto' | 'smooth'
  652. block: 'center', // 垂直对齐:'start' | 'center' | 'end' | 'nearest'
  653. inline: 'nearest', // 水平对齐:'start' | 'center' | 'end' | 'nearest'
  654. });
  655. }
  656. },
  657. },
  658. };
  659. </script>
  660. <style lang="scss" scoped>
  661. .courserware {
  662. position: relative;
  663. display: flex;
  664. flex-direction: column;
  665. row-gap: $component-spacing;
  666. width: 100%;
  667. height: 100%;
  668. min-height: calc(100vh - 226px);
  669. padding-top: $courseware-top-padding;
  670. padding-bottom: $courseware-bottom-padding;
  671. margin: 15px 0;
  672. background-color: #fff;
  673. background-repeat: no-repeat;
  674. border-bottom-right-radius: 12px;
  675. border-bottom-left-radius: 12px;
  676. &::before,
  677. &::after {
  678. position: absolute;
  679. left: 0;
  680. width: 100%;
  681. height: 15px;
  682. pointer-events: none;
  683. content: '';
  684. background: $courseware-bgColor;
  685. }
  686. &::before {
  687. top: -15px;
  688. }
  689. &::after {
  690. bottom: -15px;
  691. }
  692. .row {
  693. display: grid;
  694. gap: $component-spacing;
  695. .col {
  696. display: grid;
  697. gap: $component-spacing;
  698. overflow: hidden;
  699. }
  700. .row-checkbox {
  701. position: absolute;
  702. left: 4px;
  703. }
  704. }
  705. .custom-context-menu,
  706. .remark-info {
  707. position: absolute;
  708. z-index: 999;
  709. display: flex;
  710. gap: 3px;
  711. align-items: center;
  712. font-size: 14px;
  713. cursor: pointer;
  714. }
  715. .custom-context-menu {
  716. padding-left: 30px;
  717. background: url('../../../../assets/icon-publish.png') left center no-repeat;
  718. background-size: 24px;
  719. }
  720. .contentmenu {
  721. position: absolute;
  722. z-index: 999;
  723. display: flex;
  724. column-gap: 4px;
  725. align-items: center;
  726. padding: 8px;
  727. font-size: 14px;
  728. color: #000;
  729. background-color: #e7e7e7;
  730. border-radius: 4px;
  731. box-shadow: 0 1px 10px 0 rgba(0, 0, 0, 30%);
  732. .svg-icon,
  733. .button {
  734. cursor: pointer;
  735. }
  736. .line {
  737. min-height: 16px;
  738. margin: 0 4px;
  739. }
  740. }
  741. }
  742. </style>