mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-16 01:41:14 +08:00
cc9e2df41c
* refactor: standardized code specification * fix: rebase conflict
108 lines
2.5 KiB
TypeScript
108 lines
2.5 KiB
TypeScript
export function getDom(element) {
|
|
if (!element) {
|
|
element = document.body;
|
|
} else if (typeof element === 'string') {
|
|
element = document.querySelector(element);
|
|
}
|
|
return element;
|
|
}
|
|
|
|
export function getDomInfo(element) {
|
|
var result = {};
|
|
|
|
var dom = getDom(element);
|
|
|
|
for (var key in dom) {
|
|
var item = dom[key];
|
|
if (!item) continue;
|
|
if (typeof item === 'string' || (typeof item === 'number' && !isNaN(item))) result[key] = item;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export function getBoundingClientRect(element) {
|
|
let dom = getDom(element);
|
|
return dom.getBoundingClientRect();
|
|
}
|
|
|
|
export function addDomEventListener(element, eventName, invoker) {
|
|
let callback = args => {
|
|
const obj = {};
|
|
for (let k in args) {
|
|
obj[k] = args[k];
|
|
}
|
|
let json = JSON.stringify(obj, (k, v) => {
|
|
if (v instanceof Node) return 'Node';
|
|
if (v instanceof Window) return 'Window';
|
|
return v;
|
|
}, ' ');
|
|
invoker.invokeMethodAsync('Invoke', json);
|
|
};
|
|
|
|
if (element == 'window') {
|
|
window.addEventListener(eventName, callback);
|
|
} else {
|
|
let dom = getDom(element);
|
|
(dom as HTMLElement).addEventListener(eventName, callback);
|
|
}
|
|
}
|
|
|
|
export function matchMedia(query) {
|
|
return window.matchMedia(query).matches;
|
|
}
|
|
|
|
function fallbackCopyTextToClipboard(text) {
|
|
var textArea = document.createElement("textarea");
|
|
textArea.value = text;
|
|
|
|
// Avoid scrolling to bottom
|
|
textArea.style.top = "0";
|
|
textArea.style.left = "0";
|
|
textArea.style.position = "fixed";
|
|
|
|
document.body.appendChild(textArea);
|
|
textArea.focus();
|
|
textArea.select();
|
|
|
|
try {
|
|
var successful = document.execCommand('copy');
|
|
var msg = successful ? 'successful' : 'unsuccessful';
|
|
console.log('Fallback: Copying text command was ' + msg);
|
|
} catch (err) {
|
|
console.error('Fallback: Oops, unable to copy', err);
|
|
}
|
|
|
|
document.body.removeChild(textArea);
|
|
}
|
|
export function copy(text) {
|
|
if (!navigator.clipboard) {
|
|
fallbackCopyTextToClipboard(text);
|
|
return;
|
|
}
|
|
navigator.clipboard.writeText(text).then(function () {
|
|
console.log('Async: Copying to clipboard was successful!');
|
|
}, function (err) {
|
|
console.error('Async: Could not copy text: ', err);
|
|
});
|
|
}
|
|
|
|
export function focus(selector) {
|
|
let dom = getDom(selector);
|
|
dom.focus();
|
|
}
|
|
|
|
export function blur(selector) {
|
|
let dom = getDom(selector);
|
|
dom.blur();
|
|
}
|
|
|
|
export function log(text) {
|
|
console.log(text);
|
|
}
|
|
|
|
export function BackTop(element) {
|
|
let dom = document.getElementById("BodyContainer");
|
|
dom.scrollTo(0, 0);
|
|
}
|