Browse Source

fix markup range

RoyLiu 4 years ago
parent
commit
d57832a0d2
1 changed files with 32 additions and 24 deletions
  1. 32 24
      helpers/markup.ts

+ 32 - 24
helpers/markup.ts

@@ -108,35 +108,43 @@ export const getMarkupWithSelection: GetMarkupWithSelectionFunc = ({
       right: offsetRight,
     };
 
-    if (
+    if (startElement === ele && endElement === ele) {
+      // in same element
+      coords.left = startX;
+      coords.right = endX;
+      position.push(coords);
+    } else if (startElement === ele) {
+      // in start element
+      coords.left = startX;
+      position.push(coords);
+    } else if (endElement === ele) {
+      // in end element
+      coords.right = endX;
+      position.push(coords);
+    } else if (
       offsetTop >= startY + offsetHeight &&
       offsetBottom <= endY - offsetHeight
     ) {
+      // in row
       position.push(coords);
-    } else if (offsetTop >= startY - 2 && offsetBottom <= endY + 2) {
-      if (startElement === endElement && startElement === ele) {
-        // start and end same element
-        coords.left = startX;
-        coords.right = endX;
-        position.push(coords);
-      } else if (startElement === ele) {
-        // start element
-        coords.left = startX;
-        position.push(coords);
-      } else if (endElement === ele) {
-        // end element
-        coords.right = endX;
-        position.push(coords);
-      } else if (
-        (startX < offsetLeft && endX > offsetRight) ||
+    } else if (
+      offsetLeft > startX &&
+      offsetRight < endX &&
+      offsetTop >= startY &&
+      offsetBottom <= endY
+    ) {
+      // in line
+      position.push(coords);
+    } else if (offsetTop >= startY - 3 && offsetBottom <= endY + 3) {
+      if (
         (offsetLeft > startX &&
-          offsetTop <= startY + 2 &&
-          endY > startY + offsetHeight) ||
+          offsetTop >= startY - 3 &&
+          offsetBottom < endY - offsetHeight) ||
         (offsetRight < endX &&
-          offsetBottom >= endY - 2 &&
-          startY < endY - offsetHeight)
+          offsetBottom <= endY + 3 &&
+          offsetTop > startY + offsetHeight)
       ) {
-        // middle element
+        // in first line and end line
         position.push(coords);
       }
     }
@@ -144,8 +152,8 @@ export const getMarkupWithSelection: GetMarkupWithSelectionFunc = ({
 
   for (let i = 1; i < position.length; i += 1) {
     if (
-      position[i].top >= position[i - 1].top - 2 &&
-      position[i].bottom <= position[i - 1].bottom + 2
+      position[i].top >= position[i - 1].top - 4 &&
+      position[i].bottom <= position[i - 1].bottom + 4
     ) {
       position[i - 1].right = position[i].left;
     }