1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/// <reference path='./import.ts' /> 17class FontColorModifier extends ModifierWithKey<ResourceColor> { 18 constructor(value: ResourceColor) { 19 super(value); 20 } 21 static identity: Symbol = Symbol('textFontColor'); 22 applyPeer(node: KNode, reset: boolean): void { 23 if (reset) { 24 getUINativeModule().text.resetFontColor(node); 25 } else { 26 getUINativeModule().text.setFontColor(node, this.value); 27 } 28 } 29 30 checkObjectDiff(): boolean { 31 return !isBaseOrResourceEqual(this.stageValue, this.value); 32 } 33} 34 35class FontSizeModifier extends ModifierWithKey<number | string | Resource> { 36 constructor(value: number | string | Resource) { 37 super(value); 38 } 39 static identity: Symbol = Symbol('textFontSize'); 40 applyPeer(node: KNode, reset: boolean): void { 41 if (reset) { 42 getUINativeModule().text.resetFontSize(node); 43 } else { 44 getUINativeModule().text.setFontSize(node, this.value); 45 } 46 } 47 48 checkObjectDiff(): boolean { 49 return !isBaseOrResourceEqual(this.stageValue, this.value); 50 } 51} 52 53class FontWeightModifier extends ModifierWithKey<string> { 54 constructor(value: string) { 55 super(value); 56 } 57 static identity: Symbol = Symbol('textFontWeight'); 58 applyPeer(node: KNode, reset: boolean): void { 59 if (reset) { 60 getUINativeModule().text.resetFontWeight(node); 61 } else { 62 getUINativeModule().text.setFontWeight(node, this.value); 63 } 64 } 65} 66 67class FontStyleModifier extends ModifierWithKey<number> { 68 constructor(value: number) { 69 super(value); 70 } 71 static identity: Symbol = Symbol('textFontStyle'); 72 applyPeer(node: KNode, reset: boolean): void { 73 if (reset) { 74 getUINativeModule().text.resetFontStyle(node); 75 } else { 76 getUINativeModule().text.setFontStyle(node, this.value); 77 } 78 } 79} 80 81class TextAlignModifier extends ModifierWithKey<number> { 82 constructor(value: number) { 83 super(value); 84 } 85 static identity: Symbol = Symbol('textAlign'); 86 applyPeer(node: KNode, reset: boolean): void { 87 if (reset) { 88 getUINativeModule().text.resetTextAlign(node); 89 } else { 90 getUINativeModule().text.setTextAlign(node, this.value); 91 } 92 } 93} 94 95class TextHeightAdaptivePolicyModifier extends ModifierWithKey<TextHeightAdaptivePolicy> { 96 constructor(value: TextHeightAdaptivePolicy) { 97 super(value); 98 } 99 static identity: Symbol = Symbol('textHeightAdaptivePolicy'); 100 applyPeer(node: KNode, reset: boolean): void { 101 if (reset) { 102 getUINativeModule().text.resetHeightAdaptivePolicy(node); 103 } else { 104 getUINativeModule().text.setHeightAdaptivePolicy(node, this.value!); 105 } 106 } 107 checkObjectDiff(): boolean { 108 return !isBaseOrResourceEqual(this.stageValue, this.value); 109 } 110} 111 112class TextDraggableModifier extends ModifierWithKey<boolean> { 113 constructor(value: boolean) { 114 super(value); 115 } 116 static identity: Symbol = Symbol('textDraggable'); 117 applyPeer(node: KNode, reset: boolean): void { 118 if (reset) { 119 getUINativeModule().text.resetDraggable(node); 120 } else { 121 getUINativeModule().text.setDraggable(node, this.value!); 122 } 123 } 124 checkObjectDiff(): boolean { 125 return !isBaseOrResourceEqual(this.stageValue, this.value); 126 } 127} 128 129class TextWordBreakModifier extends ModifierWithKey<WordBreak> { 130 constructor(value: WordBreak) { 131 super(value); 132 } 133 static identity: Symbol = Symbol('textWordBreak'); 134 applyPeer(node: KNode, reset: boolean): void { 135 if (reset) { 136 getUINativeModule().text.resetWordBreak(node); 137 } else { 138 getUINativeModule().text.setWordBreak(node, this.value!); 139 } 140 } 141 checkObjectDiff(): boolean { 142 return !isBaseOrResourceEqual(this.stageValue, this.value); 143 } 144} 145 146class TextEllipsisModeModifier extends ModifierWithKey<EllipsisMode> { 147 constructor(value: EllipsisMode) { 148 super(value); 149 } 150 static identity: Symbol = Symbol('textEllipsisMode'); 151 applyPeer(node: KNode, reset: boolean): void { 152 if (reset) { 153 getUINativeModule().text.resetEllipsisMode(node); 154 } else { 155 getUINativeModule().text.setEllipsisMode(node, this.value!); 156 } 157 } 158 checkObjectDiff(): boolean { 159 return !isBaseOrResourceEqual(this.stageValue, this.value); 160 } 161} 162 163class TextMinFontSizeModifier extends ModifierWithKey<number | string | Resource> { 164 constructor(value: number | string | Resource) { 165 super(value); 166 } 167 static identity: Symbol = Symbol('textMinFontSize'); 168 applyPeer(node: KNode, reset: boolean): void { 169 if (reset) { 170 getUINativeModule().text.resetMinFontSize(node); 171 } else if (!isNumber(this.value) && !isString(this.value) && !isResource(this.value)) { 172 getUINativeModule().text.resetMinFontSize(node); 173 } else { 174 getUINativeModule().text.setMinFontSize(node, this.value!); 175 } 176 } 177 178 checkObjectDiff(): boolean { 179 return !isBaseOrResourceEqual(this.stageValue, this.value); 180 } 181} 182 183class TextMaxFontSizeModifier extends ModifierWithKey<number | string | Resource> { 184 constructor(value: number | string | Resource) { 185 super(value); 186 } 187 static identity: Symbol = Symbol('textMaxFontSize'); 188 applyPeer(node: KNode, reset: boolean): void { 189 if (reset) { 190 getUINativeModule().text.resetMaxFontSize(node); 191 } else if (!isNumber(this.value) && !isString(this.value) && !isResource(this.value)) { 192 getUINativeModule().text.resetMaxFontSize(node); 193 } else { 194 getUINativeModule().text.setMaxFontSize(node, this.value!); 195 } 196 } 197 198 checkObjectDiff(): boolean { 199 return !isBaseOrResourceEqual(this.stageValue, this.value); 200 } 201} 202 203class TextLineHeightModifier extends ModifierWithKey<number | string | Resource> { 204 constructor(value: number | string | Resource) { 205 super(value); 206 } 207 static identity: Symbol = Symbol('textLineHeight'); 208 applyPeer(node: KNode, reset: boolean): void { 209 if (reset) { 210 getUINativeModule().text.resetLineHeight(node); 211 } else if (!isNumber(this.value) && !isString(this.value) && !isResource(this.value)) { 212 getUINativeModule().text.resetLineHeight(node); 213 } else { 214 getUINativeModule().text.setLineHeight(node, this.value!); 215 } 216 } 217 218 checkObjectDiff(): boolean { 219 return !isBaseOrResourceEqual(this.stageValue, this.value); 220 } 221} 222 223class TextCopyOptionModifier extends ModifierWithKey<CopyOptions> { 224 constructor(value: CopyOptions) { 225 super(value); 226 } 227 static identity: Symbol = Symbol('textCopyOption'); 228 applyPeer(node: KNode, reset: boolean): void { 229 if (reset) { 230 getUINativeModule().text.resetCopyOption(node); 231 } else { 232 getUINativeModule().text.setCopyOption(node, this.value!); 233 } 234 } 235 checkObjectDiff(): boolean { 236 return !isBaseOrResourceEqual(this.stageValue, this.value); 237 } 238} 239 240class TextFontFamilyModifier extends ModifierWithKey<string | Resource> { 241 constructor(value: string | Resource) { 242 super(value); 243 } 244 static identity: Symbol = Symbol('textFontFamily'); 245 applyPeer(node: KNode, reset: boolean): void { 246 if (reset) { 247 getUINativeModule().text.resetFontFamily(node); 248 } else if (!isString(this.value) && !isResource(this.value)) { 249 getUINativeModule().text.resetFontFamily(node); 250 } else { 251 getUINativeModule().text.setFontFamily(node, this.value!); 252 } 253 } 254 255 checkObjectDiff(): boolean { 256 return !isBaseOrResourceEqual(this.stageValue, this.value); 257 } 258} 259 260class TextMaxLinesModifier extends ModifierWithKey<number> { 261 constructor(value: number) { 262 super(value); 263 } 264 static identity: Symbol = Symbol('textMaxLines'); 265 applyPeer(node: KNode, reset: boolean): void { 266 if (reset) { 267 getUINativeModule().text.resetMaxLines(node); 268 } else if (!isNumber(this.value)) { 269 getUINativeModule().text.resetMaxLines(node); 270 } else { 271 getUINativeModule().text.setMaxLines(node, this.value!); 272 } 273 } 274 checkObjectDiff(): boolean { 275 return !isBaseOrResourceEqual(this.stageValue, this.value); 276 } 277} 278 279class TextLetterSpacingModifier extends ModifierWithKey<number | string> { 280 constructor(value: number | string) { 281 super(value); 282 } 283 static identity: Symbol = Symbol('textLetterSpacing'); 284 applyPeer(node: KNode, reset: boolean): void { 285 if (reset) { 286 getUINativeModule().text.resetLetterSpacing(node); 287 } else if (!isNumber(this.value) && !isString(this.value)) { 288 getUINativeModule().text.resetLetterSpacing(node); 289 } else { 290 getUINativeModule().text.setLetterSpacing(node, this.value!); 291 } 292 } 293 checkObjectDiff(): boolean { 294 return !isBaseOrResourceEqual(this.stageValue, this.value); 295 } 296} 297 298class TextTextOverflowModifier extends ModifierWithKey<{ overflow: TextOverflow }> { 299 constructor(value: { overflow: TextOverflow }) { 300 super(value); 301 } 302 static identity: Symbol = Symbol('textTextOverflow'); 303 applyPeer(node: KNode, reset: boolean): void { 304 if (reset) { 305 getUINativeModule().text.resetTextOverflow(node); 306 } else { 307 getUINativeModule().text.setTextOverflow(node, this.value.overflow); 308 } 309 } 310 checkObjectDiff(): boolean { 311 return !isBaseOrResourceEqual(this.stageValue.overflow, this.value.overflow); 312 } 313} 314 315class TextBaselineOffsetModifier extends ModifierWithKey<number | string> { 316 constructor(value: number | string) { 317 super(value); 318 } 319 static identity: symbol = Symbol('textBaselineOffset'); 320 applyPeer(node: KNode, reset: boolean): void { 321 if (reset) { 322 getUINativeModule().text.resetBaselineOffset(node); 323 } else if (!isNumber(this.value) && !isString(this.value)) { 324 getUINativeModule().text.resetBaselineOffset(node); 325 } else { 326 getUINativeModule().text.setBaselineOffset(node, this.value!); 327 } 328 } 329 checkObjectDiff(): boolean { 330 return !isBaseOrResourceEqual(this.stageValue, this.value); 331 } 332} 333 334class TextTextCaseModifier extends ModifierWithKey<TextCase> { 335 constructor(value: TextCase) { 336 super(value); 337 } 338 static identity: symbol = Symbol('textTextCase'); 339 applyPeer(node: KNode, reset: boolean): void { 340 if (reset) { 341 getUINativeModule().text.resetTextCase(node); 342 } else { 343 getUINativeModule().text.setTextCase(node, this.value!); 344 } 345 } 346 checkObjectDiff(): boolean { 347 return !isBaseOrResourceEqual(this.stageValue, this.value); 348 } 349} 350 351class TextTextIndentModifier extends ModifierWithKey<Length> { 352 constructor(value: Length) { 353 super(value); 354 } 355 static identity: symbol = Symbol('textTextIndent'); 356 applyPeer(node: KNode, reset: boolean): void { 357 if (reset) { 358 getUINativeModule().text.resetTextIndent(node); 359 } else if (!isNumber(this.value) && !isString(this.value) && !isResource(this.value)) { 360 getUINativeModule().text.resetTextIndent(node); 361 } else { 362 getUINativeModule().text.setTextIndent(node, this.value!); 363 } 364 } 365 366 checkObjectDiff(): boolean { 367 return !isBaseOrResourceEqual(this.stageValue, this.value); 368 } 369} 370 371class TextTextShadowModifier extends ModifierWithKey<ShadowOptions | Array<ShadowOptions>> { 372 constructor(value: ShadowOptions | Array<ShadowOptions>) { 373 super(value); 374 } 375 static identity: Symbol = Symbol('textTextShadow'); 376 applyPeer(node: KNode, reset: boolean): void { 377 if (reset) { 378 getUINativeModule().text.resetTextShadow(node); 379 } else { 380 let shadow: ArkShadowInfoToArray = new ArkShadowInfoToArray(); 381 if (!shadow.convertShadowOptions(this.value)) { 382 getUINativeModule().text.resetTextShadow(node); 383 } else { 384 getUINativeModule().text.setTextShadow(node, shadow.radius, 385 shadow.type, shadow.color, shadow.offsetX, shadow.offsetY, shadow.fill, shadow.radius.length); 386 } 387 } 388 } 389 390 checkObjectDiff(): boolean { 391 let checkDiff = true; 392 let arkShadow = new ArkShadowInfoToArray(); 393 if (Object.getPrototypeOf(this.stageValue).constructor === Object && 394 Object.getPrototypeOf(this.value).constructor === Object) { 395 checkDiff = arkShadow.checkDiff(<ShadowOptions> this.stageValue, <ShadowOptions> this.value); 396 } else if (Object.getPrototypeOf(this.stageValue).constructor === Array && 397 Object.getPrototypeOf(this.value).constructor === Array && 398 (<Array<ShadowOptions>> this.stageValue).length === (<Array<ShadowOptions>> this.value).length) { 399 let isDiffItem = false; 400 for (let i: number = 0; i < (<Array<ShadowOptions>> this.value).length; i++) { 401 if (arkShadow.checkDiff(this.stageValue[i], this.value[1])) { 402 isDiffItem = true; 403 break; 404 } 405 } 406 if (!isDiffItem) { 407 checkDiff = false; 408 } 409 } 410 return checkDiff; 411 } 412} 413 414class TextDecorationModifier extends ModifierWithKey<{ type: TextDecorationType; color?: ResourceColor }> { 415 constructor(value: { type: TextDecorationType; color?: ResourceColor }) { 416 super(value); 417 } 418 static identity: Symbol = Symbol('textDecoration'); 419 applyPeer(node: KNode, reset: boolean): void { 420 if (reset) { 421 getUINativeModule().text.resetDecoration(node); 422 } else { 423 getUINativeModule().text.setDecoration(node, this.value!.type, this.value!.color); 424 } 425 } 426 427 checkObjectDiff(): boolean { 428 if (this.stageValue.type !== this.value.type) { 429 return true; 430 } 431 if (isResource(this.stageValue.color) && isResource(this.value.color)) { 432 return !isResourceEqual(this.stageValue.color, this.value.color); 433 } else if (!isResource(this.stageValue.color) && !isResource(this.value.color)) { 434 return !(this.stageValue.color === this.value.color); 435 } else { 436 return true; 437 } 438 } 439} 440 441class TextFontModifier extends ModifierWithKey<Font> { 442 constructor(value: Font) { 443 super(value); 444 } 445 static identity: Symbol = Symbol('textFont'); 446 applyPeer(node: KNode, reset: boolean): void { 447 if (reset) { 448 getUINativeModule().text.resetFont(node); 449 } else { 450 getUINativeModule().text.setFont(node, this.value.size, this.value.weight, this.value.family, this.value.style); 451 } 452 } 453 454 checkObjectDiff(): boolean { 455 if (this.stageValue.weight !== this.value.weight || this.stageValue.style !== this.value.style) { 456 return true; 457 } 458 if (((isResource(this.stageValue.size) && isResource(this.value.size) && 459 isResourceEqual(this.stageValue.size, this.value.size)) || 460 (!isResource(this.stageValue.size) && !isResource(this.value.size) && 461 this.stageValue.size === this.value.size)) && 462 ((isResource(this.stageValue.family) && isResource(this.value.family) && 463 isResourceEqual(this.stageValue.family, this.value.family)) || 464 (!isResource(this.stageValue.family) && !isResource(this.value.family) && 465 this.stageValue.family === this.value.family))) { 466 return false; 467 } else { 468 return true; 469 } 470 } 471} 472 473class TextClipModifier extends ModifierWithKey<boolean | object> { 474 constructor(value: boolean | object) { 475 super(value); 476 } 477 static identity: Symbol = Symbol('textClip'); 478 applyPeer(node: KNode, reset: boolean): void { 479 if (reset) { 480 getUINativeModule().common.resetClipWithEdge(node); 481 } else { 482 getUINativeModule().common.setClipWithEdge(node, this.value); 483 } 484 } 485 486 checkObjectDiff(): boolean { 487 return true; 488 } 489} 490 491class ArkTextComponent extends ArkComponent implements TextAttribute { 492 constructor(nativePtr: KNode) { 493 super(nativePtr); 494 } 495 enableDataDetector(enable: boolean): this { 496 throw new Error('Method not implemented.'); 497 } 498 dataDetectorConfig(config: any): this { 499 throw new Error('Method not implemented.'); 500 } 501 onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this { 502 throw new Error('Method not implemented.'); 503 } 504 font(value: Font): TextAttribute { 505 modifierWithKey(this._modifiersWithKeys, TextFontModifier.identity, TextFontModifier, value); 506 return this; 507 } 508 fontColor(value: ResourceColor): TextAttribute { 509 modifierWithKey(this._modifiersWithKeys, FontColorModifier.identity, FontColorModifier, value); 510 return this; 511 } 512 fontSize(value: any): TextAttribute { 513 modifierWithKey(this._modifiersWithKeys, FontSizeModifier.identity, FontSizeModifier, value); 514 return this; 515 } 516 minFontSize(value: number | string | Resource): TextAttribute { 517 modifierWithKey(this._modifiersWithKeys, TextMinFontSizeModifier.identity, TextMinFontSizeModifier, value); 518 return this; 519 } 520 maxFontSize(value: number | string | Resource): TextAttribute { 521 modifierWithKey(this._modifiersWithKeys, TextMaxFontSizeModifier.identity, TextMaxFontSizeModifier, value); 522 return this; 523 } 524 fontStyle(value: FontStyle): TextAttribute { 525 modifierWithKey(this._modifiersWithKeys, FontStyleModifier.identity, FontStyleModifier, value); 526 return this; 527 } 528 fontWeight(value: number | FontWeight | string): TextAttribute { 529 let fontWeightStr: string = '400'; 530 if (isNumber(value)) { 531 fontWeightStr = value.toString(); 532 } else if (isString(value)) { 533 fontWeightStr = String(value); 534 } 535 modifierWithKey(this._modifiersWithKeys, FontWeightModifier.identity, FontWeightModifier, fontWeightStr); 536 return this; 537 } 538 textAlign(value: TextAlign): TextAttribute { 539 modifierWithKey(this._modifiersWithKeys, TextAlignModifier.identity, TextAlignModifier, value); 540 return this; 541 } 542 lineHeight(value: number | string | Resource): TextAttribute { 543 modifierWithKey(this._modifiersWithKeys, TextLineHeightModifier.identity, TextLineHeightModifier, value); 544 return this; 545 } 546 textOverflow(value: { overflow: TextOverflow }): TextAttribute { 547 modifierWithKey(this._modifiersWithKeys, TextTextOverflowModifier.identity, TextTextOverflowModifier, value); 548 return this; 549 } 550 fontFamily(value: string | Resource): TextAttribute { 551 modifierWithKey(this._modifiersWithKeys, TextFontFamilyModifier.identity, TextFontFamilyModifier, value); 552 return this; 553 } 554 maxLines(value: number): TextAttribute { 555 modifierWithKey(this._modifiersWithKeys, TextMaxLinesModifier.identity, TextMaxLinesModifier, value); 556 return this; 557 } 558 decoration(value: { type: TextDecorationType; color?: ResourceColor }): TextAttribute { 559 modifierWithKey(this._modifiersWithKeys, TextDecorationModifier.identity, TextDecorationModifier, value); 560 return this; 561 } 562 letterSpacing(value: number | string): TextAttribute { 563 modifierWithKey(this._modifiersWithKeys, TextLetterSpacingModifier.identity, TextLetterSpacingModifier, value); 564 return this; 565 } 566 textCase(value: TextCase): TextAttribute { 567 modifierWithKey(this._modifiersWithKeys, TextTextCaseModifier.identity, TextTextCaseModifier, value); 568 return this; 569 } 570 baselineOffset(value: number | string): TextAttribute { 571 modifierWithKey(this._modifiersWithKeys, TextBaselineOffsetModifier.identity, TextBaselineOffsetModifier, value); 572 return this; 573 } 574 copyOption(value: CopyOptions): TextAttribute { 575 modifierWithKey(this._modifiersWithKeys, TextCopyOptionModifier.identity, TextCopyOptionModifier, value); 576 return this; 577 } 578 draggable(value: boolean): this { 579 modifierWithKey(this._modifiersWithKeys, TextDraggableModifier.identity, TextDraggableModifier, value); 580 return this; 581 } 582 textShadow(value: ShadowOptions | Array<ShadowOptions>): TextAttribute { 583 modifierWithKey(this._modifiersWithKeys, TextTextShadowModifier.identity, TextTextShadowModifier, value); 584 return this; 585 } 586 heightAdaptivePolicy(value: TextHeightAdaptivePolicy): TextAttribute { 587 modifierWithKey(this._modifiersWithKeys, TextHeightAdaptivePolicyModifier.identity, TextHeightAdaptivePolicyModifier, value); 588 return this; 589 } 590 textIndent(value: Length): TextAttribute { 591 modifierWithKey(this._modifiersWithKeys, TextTextIndentModifier.identity, TextTextIndentModifier, value); 592 return this; 593 } 594 wordBreak(value: WordBreak): TextAttribute { 595 modifierWithKey(this._modifiersWithKeys, TextWordBreakModifier.identity, TextWordBreakModifier, value); 596 return this; 597 } 598 onCopy(callback: (value: string) => void): TextAttribute { 599 throw new Error('Method not implemented.'); 600 } 601 selection(selectionStart: number, selectionEnd: number): TextAttribute { 602 throw new Error('Method not implemented.'); 603 } 604 ellipsisMode(value: EllipsisMode): TextAttribute { 605 modifierWithKey(this._modifiersWithKeys, TextEllipsisModeModifier.identity, TextEllipsisModeModifier, value); 606 return this; 607 } 608 clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): this { 609 modifierWithKey(this._modifiersWithKeys, TextClipModifier.identity, TextClipModifier, value); 610 return this; 611 } 612} 613// @ts-ignore 614globalThis.Text.attributeModifier = function (modifier) { 615 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 616 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 617 618 let component = this.createOrGetNode(elmtId, () => { 619 return new ArkTextComponent(nativeNode); 620 }); 621 applyUIAttributes(modifier, nativeNode, component); 622 component.applyModifierPatch(); 623}; 624