@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
328 lines (327 loc) • 11.3 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* WindowPlugin 接口定义
*/
/**
* 简单的 Storage 接口实现
*/
var SimpleStorage = /*#__PURE__*/function () {
function SimpleStorage() {
_classCallCheck(this, SimpleStorage);
_defineProperty(this, "items", {});
}
_createClass(SimpleStorage, [{
key: "length",
get: function get() {
return Object.keys(this.items).length;
}
}, {
key: "key",
value: function key(index) {
var keys = Object.keys(this.items);
return index >= 0 && index < keys.length ? keys[index] : null;
}
}, {
key: "getItem",
value: function getItem(key) {
return key in this.items ? this.items[key] : null;
}
}, {
key: "setItem",
value: function setItem(key, value) {
this.items[key] = value;
}
}, {
key: "removeItem",
value: function removeItem(key) {
delete this.items[key];
}
}, {
key: "clear",
value: function clear() {
this.items = {};
}
}]);
return SimpleStorage;
}();
/**
* 简单的 Location 类实现
*/
var SimpleLocation = /*#__PURE__*/function () {
function SimpleLocation() {
_classCallCheck(this, SimpleLocation);
_defineProperty(this, "hash", '');
_defineProperty(this, "host", 'localhost');
_defineProperty(this, "hostname", 'localhost');
_defineProperty(this, "href", 'https://localhost');
_defineProperty(this, "origin", 'https://localhost');
_defineProperty(this, "pathname", '/');
_defineProperty(this, "port", '');
_defineProperty(this, "protocol", 'https:');
_defineProperty(this, "search", '');
_defineProperty(this, "ancestorOrigins", {});
}
_createClass(SimpleLocation, [{
key: "assign",
value: function assign(url) {}
}, {
key: "reload",
value: function reload() {}
}, {
key: "replace",
value: function replace(url) {}
}, {
key: "toString",
value: function toString() {
return this.href;
}
}]);
return SimpleLocation;
}();
/**
* 简单的 Navigator 类实现
*/
var SimpleNavigator = /*#__PURE__*/function () {
function SimpleNavigator() {
_classCallCheck(this, SimpleNavigator);
_defineProperty(this, "userAgent", 'PisellOS/1.0');
_defineProperty(this, "language", 'zh-CN');
_defineProperty(this, "languages", ['zh-CN', 'en-US']);
_defineProperty(this, "onLine", true);
_defineProperty(this, "cookieEnabled", true);
// 以下属性只提供必要实现
_defineProperty(this, "appCodeName", 'PisellOS');
_defineProperty(this, "appName", 'PisellOS');
_defineProperty(this, "appVersion", '1.0');
_defineProperty(this, "platform", 'PisellOS');
_defineProperty(this, "product", 'PisellOS');
}
_createClass(SimpleNavigator, [{
key: "javaEnabled",
value: function javaEnabled() {
return false;
}
}]);
return SimpleNavigator;
}();
/**
* 简单的 History 接口实现
*/
var SimpleHistory = /*#__PURE__*/function () {
function SimpleHistory() {
_classCallCheck(this, SimpleHistory);
_defineProperty(this, "length", 0);
_defineProperty(this, "scrollRestoration", 'auto');
_defineProperty(this, "state", null);
}
_createClass(SimpleHistory, [{
key: "back",
value: function back() {}
}, {
key: "forward",
value: function forward() {}
}, {
key: "go",
value: function go() {}
}, {
key: "pushState",
value: function pushState() {}
}, {
key: "replaceState",
value: function replaceState() {}
}]);
return SimpleHistory;
}();
/**
* 简单 HTMLElement 接口
*/
/**
* 简单的 Document 类实现
*/
var SimpleDocument = /*#__PURE__*/function () {
function SimpleDocument() {
_classCallCheck(this, SimpleDocument);
// 仅实现基本功能
_defineProperty(this, "elements", {});
// Document 接口的必要属性
_defineProperty(this, "URL", 'https://localhost');
_defineProperty(this, "documentURI", 'https://localhost');
_defineProperty(this, "origin", 'https://localhost');
_defineProperty(this, "characterSet", 'UTF-8');
_defineProperty(this, "contentType", 'text/html');
_defineProperty(this, "compatMode", 'CSS1Compat');
}
_createClass(SimpleDocument, [{
key: "createElement",
value: function createElement(tagName) {
var element = {
tagName: tagName.toUpperCase(),
id: '',
className: '',
innerHTML: '',
style: {},
dataset: {},
attributes: {},
children: [],
parentElement: null,
addEventListener: function addEventListener() {},
removeEventListener: function removeEventListener() {},
getAttribute: function getAttribute(name) {
return element.attributes[name];
},
setAttribute: function setAttribute(name, value) {
element.attributes[name] = value;
},
removeAttribute: function removeAttribute(name) {
delete element.attributes[name];
},
appendChild: function appendChild(child) {
element.children.push(child);
return child;
},
removeChild: function removeChild(child) {
var index = element.children.indexOf(child);
if (index !== -1) {
element.children.splice(index, 1);
}
return child;
}
};
return element;
}
}, {
key: "getElementById",
value: function getElementById(id) {
return this.elements[id] || null;
}
}, {
key: "querySelector",
value: function querySelector() {
return null;
}
}, {
key: "querySelectorAll",
value: function querySelectorAll() {
return [];
}
}, {
key: "getElementsByTagName",
value: function getElementsByTagName() {
return [];
}
}, {
key: "getElementsByClassName",
value: function getElementsByClassName() {
return [];
}
}]);
return SimpleDocument;
}();
/**
* Window 插件实现
*/
var WindowPluginImpl = /*#__PURE__*/function () {
function WindowPluginImpl() {
_classCallCheck(this, WindowPluginImpl);
_defineProperty(this, "name", 'window');
_defineProperty(this, "version", '1.0.0');
// 定时器相关
_defineProperty(this, "setTimeout", Object.assign(function (handler, timeout) {
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
if (typeof globalThis !== 'undefined' && globalThis.setTimeout) {
return globalThis.setTimeout.apply(globalThis, [handler, timeout].concat(args));
}
// Fallback 实现,使用 Promise 模拟
var timeoutId = Date.now();
Promise.resolve().then(function () {
setTimeout(function () {
if (typeof handler === 'function') {
handler.apply(void 0, args);
} else {
eval(handler);
}
}, timeout || 0);
});
return timeoutId;
}, {
__promisify__: function __promisify__(delay, value) {
return new Promise(function (resolve) {
return setTimeout(resolve, delay, value);
});
}
}));
_defineProperty(this, "clearTimeout", function (timeout) {
if (typeof globalThis !== 'undefined' && globalThis.clearTimeout) {
globalThis.clearTimeout(timeout);
}
// Fallback 不做任何操作
});
_defineProperty(this, "setInterval", Object.assign(function (handler, timeout) {
var _this = this;
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
if (typeof globalThis !== 'undefined' && globalThis.setInterval) {
return globalThis.setInterval.apply(globalThis, [handler, timeout].concat(args));
}
// Fallback 实现,使用递归 setTimeout 模拟
var intervalId = Date.now();
var recursiveTimeout = function recursiveTimeout() {
_this.setTimeout(function () {
if (typeof handler === 'function') {
handler.apply(void 0, args);
} else {
eval(handler);
}
recursiveTimeout();
}, timeout || 0);
};
recursiveTimeout();
return intervalId;
}, {
__promisify__: function __promisify__(delay, value) {
return new Promise(function (resolve) {
return setInterval(resolve, delay, value);
});
}
}));
_defineProperty(this, "clearInterval", function (timeout) {
if (typeof globalThis !== 'undefined' && globalThis.clearInterval) {
globalThis.clearInterval(timeout);
}
// Fallback 不做任何操作
});
// 存储相关
_defineProperty(this, "localStorage", new SimpleStorage());
_defineProperty(this, "sessionStorage", new SimpleStorage());
// 浏览器环境对象
_defineProperty(this, "location", new SimpleLocation());
_defineProperty(this, "navigator", new SimpleNavigator());
_defineProperty(this, "document", new SimpleDocument());
_defineProperty(this, "history", new SimpleHistory());
}
_createClass(WindowPluginImpl, [{
key: "initialize",
value:
// 初始化方法
function initialize() {
console.log('[WindowPlugin] 初始化完成');
}
// 销毁方法
}, {
key: "destroy",
value: function destroy() {
console.log('[WindowPlugin] 已销毁');
}
}]);
return WindowPluginImpl;
}();
export default new WindowPluginImpl();