dynamicTagQueue.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { __awaiter } from "tslib";
  2. import { PromiseQueue } from "./PromiseQueue";
  3. export class DynamicTagQueue {
  4. constructor(krpanoRenderer) {
  5. // krpano 1.19 版本不支持动态插入 include,只能在文本中插入后重新加载
  6. this.queue = new PromiseQueue();
  7. this.syncTagsLoaded = false;
  8. this.syncTagStack = [];
  9. this.krpanoRenderer = krpanoRenderer;
  10. }
  11. /**
  12. * 等待 include 标签加载完成
  13. */
  14. waitIncludeLoaded(push) {
  15. return this.syncTagsLoaded
  16. ? Promise.resolve()
  17. : // 先进后出
  18. this.queue[push ? "push" : "unshift"]();
  19. }
  20. pushSyncTag(tagName, attribute) {
  21. this.syncTagStack.unshift({
  22. tagName,
  23. attribute,
  24. });
  25. }
  26. createSyncTags() {
  27. return __awaiter(this, void 0, void 0, function* () {
  28. const xmlDoc = yield this.getXMLContent();
  29. const krpanoElement = xmlDoc.querySelector("krpano");
  30. while (this.syncTagStack.length) {
  31. const tag = this.syncTagStack.pop();
  32. const element = xmlDoc.createElement(tag.tagName);
  33. for (const key in tag.attribute) {
  34. element.setAttribute(key, tag.attribute[key]);
  35. }
  36. krpanoElement === null || krpanoElement === void 0 ? void 0 : krpanoElement.insertBefore(element, null);
  37. }
  38. return xmlDoc;
  39. });
  40. }
  41. getXMLContent() {
  42. var _a;
  43. return __awaiter(this, void 0, void 0, function* () {
  44. let contentText = "";
  45. const xml = (_a = this.krpanoRenderer) === null || _a === void 0 ? void 0 : _a.get("xml");
  46. const parser = new DOMParser();
  47. if (xml.content) {
  48. contentText = xml.content;
  49. }
  50. else if (xml.url) {
  51. contentText = yield fetch(xml.url).then((res) => res.text());
  52. }
  53. return parser.parseFromString(contentText, "text/xml");
  54. });
  55. }
  56. }