@blocksuite/global
Advanced tools
Comparing version 0.4.0-20230203234453-6122cfe to 0.4.0-20230204233304-6e37e29
export const isWeb = typeof window !== 'undefined'; | ||
export const isFirefox = isWeb && navigator.userAgent.toLowerCase().indexOf('firefox') > -1; | ||
const withHidePopup = (fn) => { | ||
const map = new Map(); | ||
const formatBar = document.querySelector('format-quick-bar'); | ||
if (formatBar) { | ||
map.set(formatBar, formatBar.style.visibility); | ||
formatBar.style.visibility = 'hidden'; | ||
} | ||
try { | ||
return fn(); | ||
} | ||
finally { | ||
// recover | ||
map.forEach((visibility, node) => { | ||
node.style.visibility = visibility; | ||
}); | ||
} | ||
}; | ||
export function caretRangeFromPoint(clientX, clientY) { | ||
if (isFirefox) { | ||
const caret = document.caretPositionFromPoint(clientX, clientY); | ||
// TODO handle caret is covered by popup | ||
const range = document.createRange(); | ||
@@ -10,4 +28,17 @@ range.setStart(caret.offsetNode, caret.offset); | ||
} | ||
return document.caretRangeFromPoint(clientX, clientY); | ||
const range = document.caretRangeFromPoint(clientX, clientY); | ||
if (!range) { | ||
return null; | ||
} | ||
// This is a workaround | ||
// Sometimes the point be covered by the format bar, | ||
// so the range will return the body element, | ||
// which is not what we want. | ||
if (range.startContainer === document.body && | ||
range.endContainer === document.body) { | ||
const retryRange = withHidePopup(() => document.caretRangeFromPoint(clientX, clientY)); | ||
return retryRange; | ||
} | ||
return range; | ||
} | ||
//# sourceMappingURL=web.js.map |
{ | ||
"name": "@blocksuite/global", | ||
"version": "0.4.0-20230203234453-6122cfe", | ||
"version": "0.4.0-20230204233304-6e37e29", | ||
"types": "./index.d.ts", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -17,2 +17,19 @@ export const isWeb = typeof window !== 'undefined'; | ||
const withHidePopup = <T>(fn: () => T): T => { | ||
const map = new Map<HTMLElement, string>(); | ||
const formatBar = document.querySelector<HTMLElement>('format-quick-bar'); | ||
if (formatBar) { | ||
map.set(formatBar, formatBar.style.visibility); | ||
formatBar.style.visibility = 'hidden'; | ||
} | ||
try { | ||
return fn(); | ||
} finally { | ||
// recover | ||
map.forEach((visibility, node) => { | ||
node.style.visibility = visibility; | ||
}); | ||
} | ||
}; | ||
export function caretRangeFromPoint( | ||
@@ -24,2 +41,3 @@ clientX: number, | ||
const caret = document.caretPositionFromPoint(clientX, clientY); | ||
// TODO handle caret is covered by popup | ||
const range = document.createRange(); | ||
@@ -29,3 +47,20 @@ range.setStart(caret.offsetNode, caret.offset); | ||
} | ||
return document.caretRangeFromPoint(clientX, clientY); | ||
const range = document.caretRangeFromPoint(clientX, clientY); | ||
if (!range) { | ||
return null; | ||
} | ||
// This is a workaround | ||
// Sometimes the point be covered by the format bar, | ||
// so the range will return the body element, | ||
// which is not what we want. | ||
if ( | ||
range.startContainer === document.body && | ||
range.endContainer === document.body | ||
) { | ||
const retryRange = withHidePopup(() => | ||
document.caretRangeFromPoint(clientX, clientY) | ||
); | ||
return retryRange; | ||
} | ||
return range; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
242403
2995