TagActionProxy.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { __awaiter } from "tslib";
  2. import { PromiseQueue } from "./PromiseQueue";
  3. export class TagActionProxy {
  4. constructor(krpanoRenderer) {
  5. this.queue = new PromiseQueue();
  6. /**
  7. * 同步标签是否加载完成
  8. */
  9. this.syncTagsLoaded = false;
  10. this.syncTagStack = [];
  11. this.syncXMLStringStack = [];
  12. this.krpanoRenderer = krpanoRenderer;
  13. }
  14. /**
  15. * 等待 include 标签加载完成
  16. */
  17. waitIncludeLoaded(push) {
  18. return this.syncTagsLoaded
  19. ? Promise.resolve()
  20. : // 先进后出
  21. this.queue[push ? "push" : "unshift"]();
  22. }
  23. /**
  24. * 将异步标签推入堆中
  25. */
  26. pushSyncTag(tagName, attribute, children) {
  27. this.syncTagStack.unshift({
  28. tagName,
  29. attribute,
  30. children,
  31. });
  32. }
  33. /**
  34. * 创建一个插入同步标签后的 XMLDOM
  35. */
  36. createSyncTags() {
  37. return __awaiter(this, void 0, void 0, function* () {
  38. const xmlDoc = yield this.getXMLContent();
  39. const krpanoElement = xmlDoc.querySelector("krpano");
  40. while (this.syncTagStack.length) {
  41. let element = null;
  42. const tag = this.syncTagStack.pop();
  43. if (!tag.children) {
  44. element = xmlDoc.createElement(tag.tagName);
  45. }
  46. else {
  47. const parser = new DOMParser();
  48. element = parser.parseFromString(`<${tag.tagName}>${tag.children}</${tag.tagName}>`, "text/xml").documentElement;
  49. }
  50. for (const key in tag.attribute) {
  51. element.setAttribute(key, tag.attribute[key]);
  52. }
  53. krpanoElement === null || krpanoElement === void 0 ? void 0 : krpanoElement.insertBefore(element, null);
  54. }
  55. return xmlDoc;
  56. });
  57. }
  58. getXMLContent() {
  59. var _a;
  60. return __awaiter(this, void 0, void 0, function* () {
  61. let contentText = "";
  62. const xml = (_a = this.krpanoRenderer) === null || _a === void 0 ? void 0 : _a.get("xml");
  63. const parser = new DOMParser();
  64. if (xml.content) {
  65. contentText = xml.content;
  66. }
  67. else if (xml.url) {
  68. contentText = yield fetch(xml.url).then((res) => res.text());
  69. }
  70. return parser.parseFromString(contentText, "text/xml");
  71. });
  72. }
  73. }