tav-media
Version:
Cross platform media editing framework
214 lines (213 loc) • 8.52 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { tav } from '../tav';
import { MovieClip } from './tav-movie-clip';
import { allowCallNativeAnytime } from '../types/tav-object';
import { FileSystem } from '../io/file-system';
import { Path } from '../io/path';
/**
* A sticker that display PAG file
* @category Clips
*/
export class PAGSticker extends MovieClip {
constructor() {
super(...arguments);
this.type = 'PAGSticker';
this._imageReplacements = [];
this._colorReplacements = [];
this._textReplacements = [];
}
/**
* Creates a PAG sticker object from a asset, return null if the asset does
* not exist or it's not a valid asset.
* @param asset The asset used to create the sticker.
* @param contentStartTime The start time of the content in the asset.
* @param contentDuration The duration of the content in the asset.
* @param stretchDuration Set the duration of the PAGFile, stretch it to this duration.
*/
static MakeFromAsset(asset, contentStartTime, contentDuration, stretchDuration) {
return __awaiter(this, void 0, void 0, function* () {
if (!(asset === null || asset === void 0 ? void 0 : asset.path)) {
return null;
}
const pagSticker = new PAGSticker();
pagSticker.contentStartTime = contentStartTime;
pagSticker.contentDuration = contentDuration;
pagSticker._fileDuration = stretchDuration;
pagSticker._asset = asset;
return pagSticker;
});
}
clone() {
const newPagSticker = new PAGSticker();
const _a = this, { _nativeObject: _nativeClip, parent, id, nativeInvalidated } = _a, otherProperties = __rest(_a, ["_nativeObject", "parent", "id", "nativeInvalidated"]);
return Object.assign(newPagSticker, otherProperties);
}
createClip() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.asset || !this.asset.path)
return undefined;
const nativeAsset = yield this.asset.build();
if (tav.PAGSticker.MakeFromAssetStartTimeDuration) {
return yield tav.PAGSticker.MakeFromAssetStartTimeDuration(nativeAsset, this.contentStartTime || 0, this.contentDuration || this.duration || 0, this._fileDuration || this.getFileDuration());
}
if (tav.PAGSticker.MakeFrom) {
return yield tav.PAGSticker.MakeFrom(nativeAsset);
}
});
}
updateClip(pagSticker) {
const _super = Object.create(null, {
updateClip: { get: () => super.updateClip }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.updateClip.call(this, pagSticker);
yield this.replaceImages(pagSticker);
yield this.replaceTexts(pagSticker);
yield this.replaceColors(pagSticker);
});
}
replaceImages(pagSticker) {
return __awaiter(this, void 0, void 0, function* () {
for (const r of this.imageReplacements) {
if (r.imagePath !== 'NoProcessing') {
const localPath = Path.getLocalFileName(r.imagePath);
yield FileSystem.download(localPath, r.imagePath);
yield (pagSticker === null || pagSticker === void 0 ? void 0 : pagSticker.replaceImage(r.editableIndex, localPath, r.scaleMode));
}
}
;
});
}
replaceTexts(pagSticker) {
return __awaiter(this, void 0, void 0, function* () {
this.textReplacements.forEach((r) => {
if (typeof r.content === 'string') {
pagSticker.replaceText(r.editableIndex, r.content);
}
else {
pagSticker.replaceText_attribute(r.editableIndex, r.content);
}
});
});
}
replaceColors(pagSticker) {
return __awaiter(this, void 0, void 0, function* () {
this.colorReplacements.forEach((r) => {
tav.PAGSticker.replaceColor(pagSticker, r.layerIndex, r.backgroundColor);
});
});
}
/**
* Get or set a list of PAGImageReplacement to replace images in the PAG file
*/
get imageReplacements() {
return this._imageReplacements;
}
set imageReplacements(imgRs) {
this._imageReplacements = imgRs;
if (!this.nativeClip) {
this.invalidated();
;
return;
}
this.replaceImages(this.nativeClip);
}
/**
* Get or set a list of PAGTextReplacement to replace texts in the PAG file
*/
get textReplacements() {
return this._textReplacements;
}
set textReplacements(txtRs) {
this._textReplacements = txtRs;
if (!this.nativeClip) {
this.invalidated();
;
return;
}
this.replaceTexts(this.nativeClip);
}
/**
* Get or set a list of PAGColorReplacement to replace layer background colors in the PAG file
*/
get colorReplacements() {
return this._colorReplacements;
}
set colorReplacements(txtRs) {
this._colorReplacements = txtRs;
if (!this.nativeClip) {
this.invalidated();
;
return;
}
this.replaceColors(this.nativeClip);
}
/**
* The count of replaceable images.
*/
get numImages() {
var _a;
return ((_a = this.nativeClip) === null || _a === void 0 ? void 0 : _a.numImages()) || 0;
}
/**
* The count of replaceable texts.
*/
get numTexts() {
var _a;
return ((_a = this.nativeClip) === null || _a === void 0 ? void 0 : _a.numTexts()) || 0;
}
/**
* Get the text data of the specified layer index.
* @param editableIndex The index of the text layer.
*/
getTextAttribute(editableIndex) {
var _a;
return (_a = this.nativeClip) === null || _a === void 0 ? void 0 : _a.getTextAttribute(editableIndex);
}
getFileDuration() {
return Math.max(this.duration || 0, this.contentDuration || 0);
}
}
__decorate([
allowCallNativeAnytime
], PAGSticker.prototype, "replaceImages", null);
__decorate([
allowCallNativeAnytime
], PAGSticker.prototype, "replaceTexts", null);
__decorate([
allowCallNativeAnytime
], PAGSticker.prototype, "replaceColors", null);
__decorate([
allowCallNativeAnytime
], PAGSticker.prototype, "numImages", null);
__decorate([
allowCallNativeAnytime
], PAGSticker.prototype, "numTexts", null);
__decorate([
allowCallNativeAnytime
], PAGSticker.prototype, "getTextAttribute", null);