@tarojs/runtime 4.1.12-alpha.4 → 4.1.12-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -3233,7 +3233,13 @@ function setInnerHTML(element, html) {
3233
3233
  while (element.firstChild) {
3234
3234
  element.removeChild(element.firstChild);
3235
3235
  }
3236
- const children = parser(html, element.ownerDocument);
3236
+ let { ownerDocument } = element;
3237
+ if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
3238
+ if ('appDocument' in tt) {
3239
+ ownerDocument = tt.appDocument;
3240
+ }
3241
+ }
3242
+ const children = parser(html, ownerDocument);
3237
3243
  for (let i = 0; i < children.length; i++) {
3238
3244
  element.appendChild(children[i]);
3239
3245
  }
@@ -3516,6 +3522,14 @@ function eventHandler(event) {
3516
3522
  }
3517
3523
  }
3518
3524
  }
3525
+ function eventHandlerTTDom(ele, listener, event) {
3526
+ Object.assign(event, {
3527
+ mpEvent: event,
3528
+ bubbles: true,
3529
+ cancelable: true,
3530
+ });
3531
+ listener(event, ele);
3532
+ }
3519
3533
 
3520
3534
  class FormElement extends TaroElement {
3521
3535
  get type() {
@@ -3947,8 +3961,145 @@ function createDocument() {
3947
3961
  doc.body = body;
3948
3962
  return doc;
3949
3963
  }
3964
+ function createTTDomDocument() {
3965
+ var _a;
3966
+ const document = tt === null || tt === void 0 ? void 0 : tt.appDocument;
3967
+ if (!document) {
3968
+ throw new Error('tt.appDocument is not found');
3969
+ }
3970
+ const html = document.createElement(HTML);
3971
+ const head = document.createElement(HEAD);
3972
+ const body = document.createElement(BODY);
3973
+ const app = document.createElement(APP);
3974
+ app.id = APP;
3975
+ const container = document.createElement(CONTAINER);
3976
+ const emptyFunction = () => { };
3977
+ document.childNodes.push(html);
3978
+ html.childNodes.push(head, body);
3979
+ body.childNodes.push(container);
3980
+ container.childNodes.push(app);
3981
+ document.documentElement = html;
3982
+ document.head = head;
3983
+ document.body = body;
3984
+ document.appElement = app;
3985
+ let builtInComponents = (_a = tt === null || tt === void 0 ? void 0 : tt.getBuiltInComponents) === null || _a === void 0 ? void 0 : _a.call(tt);
3986
+ if (Array.isArray(builtInComponents)) {
3987
+ builtInComponents = new Set(builtInComponents);
3988
+ }
3989
+ else if (!(builtInComponents instanceof Set)) {
3990
+ builtInComponents = new Set([...shared.DEFAULT_COMPONENTS, ...shared.TT_SPECIFIC_COMPONENTS]);
3991
+ }
3992
+ document.getElementById = function getElementById(id) {
3993
+ if (id === 'app') {
3994
+ return app;
3995
+ }
3996
+ else {
3997
+ return Object.getPrototypeOf(this).getElementById.call(this, id);
3998
+ }
3999
+ };
4000
+ document.getLastPage = function getLastPage() {
4001
+ let last;
4002
+ for (const v of this._pageDocumentMap.values())
4003
+ last = v;
4004
+ return last;
4005
+ };
4006
+ document.createElement = function (type, ...args) {
4007
+ if (type === 'root') {
4008
+ return this.getLastPage();
4009
+ }
4010
+ else {
4011
+ const el = builtInComponents.has(type)
4012
+ ? Object.getPrototypeOf(this).createElement.call(this, type, ...args)
4013
+ : Object.getPrototypeOf(this).createNativeComponent.call(this, type, {
4014
+ __tt__inner__options__: {
4015
+ name: type,
4016
+ },
4017
+ });
4018
+ // 给元素加上 scopeId
4019
+ el.setAttribute('class', '');
4020
+ // 保存原始的 setAttribute 和 removeAttribute
4021
+ const originalSetAttribute = el.setAttribute.bind(el);
4022
+ const originalRemoveAttribute = el.removeAttribute.bind(el);
4023
+ // 拦截 setAttribute 来处理 catchMove
4024
+ el.setAttribute = function (name, value) {
4025
+ const result = originalSetAttribute(name, value);
4026
+ // 处理 catchMove 属性
4027
+ if (name === 'catchMove') {
4028
+ if (value) {
4029
+ el.addEventListener('catchtouchmove', emptyFunction);
4030
+ }
4031
+ else {
4032
+ el.removeEventListener('catchtouchmove', emptyFunction);
4033
+ }
4034
+ }
4035
+ return result;
4036
+ };
4037
+ // 拦截 removeAttribute 来处理 catchMove
4038
+ el.removeAttribute = function (name) {
4039
+ const oldValue = el.getAttribute(name);
4040
+ // 处理 catchMove 属性
4041
+ if (name === 'catchMove' && oldValue) {
4042
+ el.removeEventListener('catchtouchmove', emptyFunction);
4043
+ }
4044
+ return originalRemoveAttribute(name);
4045
+ };
4046
+ if (process.env.FRAMEWORK === 'preact') {
4047
+ const ttEventListener = el.addEventListener.bind(el);
4048
+ const ttRemoveEventListener = el.removeEventListener.bind(el);
4049
+ el.addEventListener = function (type, listener) {
4050
+ if (type === 'click') {
4051
+ type = 'tap';
4052
+ }
4053
+ const bindEventName = type.startsWith('bind') || type.startsWith('catch') ? type : `bind${type}`;
4054
+ // 创建包装函数
4055
+ const wrapper = (event) => {
4056
+ const type = event.type;
4057
+ // 对齐 modifyMpEvent 处理逻辑
4058
+ if (type === 'tap') {
4059
+ event.type = 'click';
4060
+ }
4061
+ else if (type === 'focus') {
4062
+ event.type = 'focusin';
4063
+ }
4064
+ else if (type === 'blur') {
4065
+ event.type = 'focusout';
4066
+ }
4067
+ Object.assign(event, {
4068
+ mpEvent: event,
4069
+ bubbles: true,
4070
+ cancelable: true,
4071
+ });
4072
+ listener.call(el, event);
4073
+ };
4074
+ // 保存包装函数的引用,用于后续移除
4075
+ if (!el.__eventWrappers) {
4076
+ el.__eventWrappers = new WeakMap();
4077
+ }
4078
+ el.__eventWrappers.set(listener, wrapper);
4079
+ ttEventListener(bindEventName, wrapper);
4080
+ };
4081
+ el.removeEventListener = function (type, listener) {
4082
+ var _a;
4083
+ if (type === 'click') {
4084
+ type = 'tap';
4085
+ }
4086
+ const bindEventName = type.startsWith('bind') || type.startsWith('catch') ? type : `bind${type}`;
4087
+ // 获取之前保存的包装函数
4088
+ const wrapper = (_a = el.__eventWrappers) === null || _a === void 0 ? void 0 : _a.get(listener);
4089
+ if (wrapper) {
4090
+ ttRemoveEventListener(bindEventName, wrapper);
4091
+ delete el.__eventWrappers[listener];
4092
+ }
4093
+ };
4094
+ }
4095
+ return el;
4096
+ }
4097
+ };
4098
+ return document;
4099
+ }
3950
4100
  // Note: 小程序端 vite 打包成 commonjs,const document = xxx 会报错,所以把 document 改为 taroDocumentProvider
3951
- const taroDocumentProvider = process.env.TARO_PLATFORM === 'web' ? env.document : (env.document = createDocument());
4101
+ const taroDocumentProvider = process.env.TARO_PLATFORM === 'web' ? env.document : (env.document =
4102
+ shared.isEnableTTDom() ? createTTDomDocument() : createDocument());
3952
4103
 
3953
4104
  // for Vue3
3954
4105
  class SVGElement extends TaroElement {
@@ -4055,13 +4206,23 @@ function createPageConfig(component, pageName, data, pageConfig) {
4055
4206
  }
4056
4207
  const mount = () => {
4057
4208
  Current.app.mount(component, $taroPath, () => {
4058
- pageElement = env.document.getElementById($taroPath);
4209
+ if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4210
+ pageElement = env.document.getPageDocumentById(this.__webviewId__);
4211
+ }
4212
+ else {
4213
+ pageElement = env.document.getElementById($taroPath);
4214
+ }
4059
4215
  shared.ensure(pageElement !== null, '没有找到页面实例。');
4060
4216
  safeExecute($taroPath, ON_LOAD, this.$taroParams);
4061
4217
  loadResolver();
4062
4218
  if (process.env.TARO_PLATFORM !== 'web') {
4063
4219
  pageElement.ctx = this;
4064
- pageElement.performUpdate(true, cb);
4220
+ if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4221
+ pageElement.sync();
4222
+ }
4223
+ else {
4224
+ pageElement.performUpdate(true, cb);
4225
+ }
4065
4226
  }
4066
4227
  else {
4067
4228
  shared.isFunction(cb) && cb();
@@ -4222,7 +4383,9 @@ function createComponentConfig(component, componentName, data) {
4222
4383
  safeExecute(path, ON_LOAD);
4223
4384
  if (process.env.TARO_PLATFORM !== 'web') {
4224
4385
  componentElement.ctx = this;
4225
- componentElement.performUpdate(true);
4386
+ if (process.env.TARO_ENV !== 'tt' || !shared.isEnableTTDom()) {
4387
+ componentElement.performUpdate(true);
4388
+ }
4226
4389
  }
4227
4390
  });
4228
4391
  },
@@ -4255,6 +4418,9 @@ function createRecursiveComponentConfig(componentName) {
4255
4418
  ? {
4256
4419
  [ATTACHED]() {
4257
4420
  var _a, _b;
4421
+ if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4422
+ return;
4423
+ }
4258
4424
  const componentId = ((_a = this.data.i) === null || _a === void 0 ? void 0 : _a.sid) || ((_b = this.props.i) === null || _b === void 0 ? void 0 : _b.sid);
4259
4425
  if (shared.isString(componentId)) {
4260
4426
  customWrapperCache.set(componentId, this);
@@ -4266,6 +4432,9 @@ function createRecursiveComponentConfig(componentName) {
4266
4432
  },
4267
4433
  [DETACHED]() {
4268
4434
  var _a, _b;
4435
+ if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4436
+ return;
4437
+ }
4269
4438
  const componentId = ((_a = this.data.i) === null || _a === void 0 ? void 0 : _a.sid) || ((_b = this.props.i) === null || _b === void 0 ? void 0 : _b.sid);
4270
4439
  if (shared.isString(componentId)) {
4271
4440
  customWrapperCache.delete(componentId);
@@ -5193,6 +5362,7 @@ exports.document = taroDocumentProvider;
5193
5362
  exports.env = env;
5194
5363
  exports.eventCenter = eventCenter;
5195
5364
  exports.eventHandler = eventHandler;
5365
+ exports.eventHandlerTTDom = eventHandlerTTDom;
5196
5366
  exports.eventSource = eventSource;
5197
5367
  exports.extend = extend;
5198
5368
  exports.getComponentsAlias = getComponentsAlias;
@@ -5225,6 +5395,7 @@ exports.perf = perf;
5225
5395
  exports.removePageInstance = removePageInstance;
5226
5396
  exports.requestAnimationFrame = _raf;
5227
5397
  exports.safeExecute = safeExecute;
5398
+ exports.setInnerHTML = setInnerHTML;
5228
5399
  exports.shortcutAttr = shortcutAttr;
5229
5400
  exports.stringify = stringify;
5230
5401
  exports.stripBasename = stripBasename;