offices-viewer
Version:
## Current Renderable File Types
109 lines (107 loc) • 3.81 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/fetch-wrapper.tsx
var fetch_wrapper_exports = {};
__export(fetch_wrapper_exports, {
default: () => fetch_wrapper_default
});
module.exports = __toCommonJS(fetch_wrapper_exports);
var import_react = __toESM(require("react"));
var Error2 = ({ error }) => {
return /* @__PURE__ */ import_react.default.createElement("div", null, error);
};
var Loading = () => {
return /* @__PURE__ */ import_react.default.createElement("div", null, "文件加载中 ……");
};
function withFetching(WrappedComponent, props) {
return class extends import_react.Component {
constructor(props2) {
super(props2);
this.state = {};
this.xhr = this.createRequest(props2.filePath);
}
componentDidMount() {
try {
this.fetch();
} catch (e) {
if (this.props.onError) {
this.props.onError(e);
}
this.setState({ error: "fetch error" });
}
}
componentWillUnmount() {
this.abort();
}
createRequest(path) {
let xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open("GET", path, true);
} else if (typeof XDomainRequest !== "undefined") {
xhr = new XDomainRequest();
xhr.open("GET", path);
} else {
xhr = null;
return null;
}
if (props.responseType) {
xhr.responseType = props.responseType;
}
xhr.onload = () => {
if (xhr.status >= 400) {
this.setState({ error: `fetch error with status ${xhr.status}` });
return;
}
const resp = props.responseType ? xhr.response : xhr.responseText;
this.setState({ data: resp });
};
return xhr;
}
fetch() {
this.xhr.send();
}
abort() {
if (this.xhr) {
this.xhr.abort();
}
}
render() {
if (!this.xhr) {
return /* @__PURE__ */ import_react.default.createElement("h1", null, "CORS not supported..");
}
if (this.state.error) {
return /* @__PURE__ */ import_react.default.createElement(Error2, { ...this.props, error: this.state.error });
}
if (this.state.data) {
return /* @__PURE__ */ import_react.default.createElement(WrappedComponent, { data: this.state.data, ...this.props });
}
return /* @__PURE__ */ import_react.default.createElement(Loading, null);
}
};
}
var fetch_wrapper_default = withFetching;