ant-design-blazor/components/core/JsInterop/interop.ts

76 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-03-11 16:49:25 +08:00
export function getDom(element) {
if (!element) {
element = document.body;
} else if (typeof element === 'string') {
element = document.querySelector(element);
}
return element;
}
2020-03-10 15:25:29 +08:00
2020-03-11 16:49:25 +08:00
export function getDomInfo(element) {
var result = {};
2020-03-10 15:25:29 +08:00
2020-03-11 16:49:25 +08:00
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;
}
2020-03-10 15:25:29 +08:00
2020-03-11 16:49:25 +08:00
return result;
2020-03-10 15:25:29 +08:00
}
export function getBoundingClientRect(element) {
2020-03-11 16:49:25 +08:00
let dom = getDom(element);
return dom.getBoundingClientRect();
2020-03-10 15:25:29 +08:00
}
2020-03-11 16:49:25 +08:00
export function addDomEventListener(element, eventName, invoker) {
let callback = args => {
invoker.invokeMethodAsync('Invoke', args);
};
2020-03-11 16:40:07 +08:00
2020-03-11 16:49:25 +08:00
if (element == 'window') {
window.addEventListener(eventName, callback);
} else {
let dom = getDom(element);
(document.querySelector(dom) as HTMLElement).addEventListener(eventName, callback);
}
2020-03-10 15:25:29 +08:00
}
export function antMatchMedia(query) {
2020-03-11 16:49:25 +08:00
return window.matchMedia(query).matches;
2020-03-10 15:25:29 +08:00
}
export function copy(text): Promise<any> {
2020-03-11 16:49:25 +08:00
return new Promise<any>((resolve, reject) => {
let copyTextArea = null;
try {
copyTextArea = this.document.createElement('textarea');
copyTextArea.style.all = 'unset';
copyTextArea.style.position = 'fixed';
copyTextArea.style.top = '0';
copyTextArea.style.clip = 'rect(0, 0, 0, 0)';
copyTextArea.style.whiteSpace = 'pre';
copyTextArea.style.webkitUserSelect = 'text';
copyTextArea.style.MozUserSelect = 'text';
copyTextArea.style.msUserSelect = 'text';
copyTextArea.style.userSelect = 'text';
this.document.body.appendChild(copyTextArea);
copyTextArea.value = text;
copyTextArea.select();
2020-03-10 15:25:29 +08:00
2020-03-11 16:49:25 +08:00
const successful = this.document.execCommand('copy');
if (!successful) {
reject(text);
}
resolve(text);
} finally {
if (copyTextArea) {
this.document.body.removeChild(copyTextArea);
}
}
});
}