|
@@ -88,8 +88,20 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
const getSelectText = () => {
|
|
|
const selection: any = window.getSelection();
|
|
|
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
|
|
|
-
|
|
|
-
|
|
|
+ // if (range) {
|
|
|
+ // const startNode = range.startContainer;
|
|
|
+ // let currentParagraph = startNode;
|
|
|
+ // while (
|
|
|
+ // currentParagraph.nodeType !== Node.ELEMENT_NODE ||
|
|
|
+ // currentParagraph.tagName !== 'P'
|
|
|
+ // ) {
|
|
|
+ // currentParagraph = currentParagraph.parentNode;
|
|
|
+ // }
|
|
|
+ // range.setEnd(currentParagraph, currentParagraph.childNodes.length);
|
|
|
+ // selection.removeAllRanges();
|
|
|
+ // selection.addRange(range);
|
|
|
+ // }
|
|
|
+ // console.log(selection, 'selection.selectAllChildren()')
|
|
|
if (selection.toString().length > 0) {
|
|
|
state.showDom = true;
|
|
|
const textContainer: any = document.querySelector(_musicContent);
|
|
@@ -99,7 +111,10 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
anchorOffset = 0,
|
|
|
endIndex = 0,
|
|
|
focusOffset = 0;
|
|
|
- console.log(selection, 'selection');
|
|
|
+ // console.log(selection, 'selection');
|
|
|
+ // sentences.forEach((item: any) => {
|
|
|
+ // console.log(selection.containsNode(item), '---', item)
|
|
|
+ // })
|
|
|
|
|
|
// 都为0的情况下判断为选中某一个段
|
|
|
if (selection.focusOffset === 0 && selection.anchorOffset === 0) {
|
|
@@ -110,6 +125,7 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
const firstNode = startParagraph.childNodes[0];
|
|
|
const endNode =
|
|
|
startParagraph.childNodes[startParagraph.childNodes.length - 1];
|
|
|
+
|
|
|
sentences?.forEach((element: any, index: number) => {
|
|
|
if (element === firstNode) {
|
|
|
startIndex = index;
|
|
@@ -121,15 +137,16 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
+ const direction = checkSelectionDirection(selection);
|
|
|
const firstNode =
|
|
|
- checkSelectionDirection(selection) === 'up'
|
|
|
+ direction === 'up'
|
|
|
? selection.anchorNode.parentNode
|
|
|
: selection.focusNode.parentNode;
|
|
|
const endNode =
|
|
|
- checkSelectionDirection(selection) === 'down'
|
|
|
+ direction === 'down'
|
|
|
? selection.anchorNode.parentNode
|
|
|
: selection.focusNode.parentNode;
|
|
|
- if (checkSelectionDirection(selection) === 'up') {
|
|
|
+ if (direction === 'up') {
|
|
|
anchorOffset = selection.anchorOffset;
|
|
|
focusOffset = selection.focusOffset;
|
|
|
} else {
|
|
@@ -140,19 +157,20 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
if (element === firstNode) {
|
|
|
startIndex = index;
|
|
|
anchorOffset =
|
|
|
- checkSelectionDirection(selection) === 'up'
|
|
|
+ direction === 'up'
|
|
|
? selection.anchorOffset
|
|
|
: selection.focusOffset;
|
|
|
}
|
|
|
if (element === endNode) {
|
|
|
endIndex = index;
|
|
|
focusOffset =
|
|
|
- checkSelectionDirection(selection) === 'down'
|
|
|
+ direction === 'down'
|
|
|
? selection.anchorOffset
|
|
|
: selection.focusOffset;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
state.selectOptions.startIndex = startIndex;
|
|
|
state.selectOptions.anchorOffset = anchorOffset;
|
|
|
state.selectOptions.endIndex = endIndex;
|
|
@@ -174,7 +192,7 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
const rects = range.getClientRects();
|
|
|
|
|
|
if (rects.length > 0) {
|
|
|
- // console.log(rects, 'rects');
|
|
|
+ // console.log(rects, 'rects', range);
|
|
|
const firstRect = rects[rects.length - 1];
|
|
|
const x = firstRect.right;
|
|
|
const y = firstRect.top;
|
|
@@ -203,20 +221,6 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
showDom.style.left = 'auto';
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // if (parentRect?.width - (x - parentRect?.left) > showDomRect.width) {
|
|
|
- // // 判断是否选择到最右边 超出边界
|
|
|
- // // showDom.style.left = (x - parentRect?.right).toFixed(2) + 'px';
|
|
|
- // // showDom.style.right = 'auto';
|
|
|
- // showDom.style.right = (parentRect?.right - x - 6).toFixed(2) + 'px';
|
|
|
- // showDom.style.left = 'auto';
|
|
|
- // } else {
|
|
|
- // // showDom.style.right = '0px';
|
|
|
- // // showDom.style.left = 'auto';
|
|
|
-
|
|
|
- // showDom.style.right = 'auto';
|
|
|
- // showDom.style.left = '0';
|
|
|
- // }
|
|
|
}
|
|
|
}
|
|
|
};
|
|
@@ -355,7 +359,6 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
|
|
|
// 初始化高亮显示
|
|
|
const initLightText = () => {
|
|
|
-
|
|
|
sentences?.forEach((sentence: any, i: number) => {
|
|
|
if (i >= currentSentenceIndex && i <= end) {
|
|
|
const text = sentence.textContent;
|
|
@@ -397,8 +400,6 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- initLightText();
|
|
|
-
|
|
|
// 开始播放
|
|
|
const speaker = () => {
|
|
|
try {
|
|
@@ -490,6 +491,8 @@ export const useSpeak = (musicContent?: string, selectionCouser?: string) => {
|
|
|
};
|
|
|
|
|
|
speaker();
|
|
|
+
|
|
|
+ initLightText();
|
|
|
};
|
|
|
|
|
|
const onDestory = () => {
|