fcr-core
Version:
Core APIs for building online scenes
294 lines (293 loc) • 17.7 kB
JavaScript
import "core-js/modules/es.array.push.js";
import "core-js/modules/esnext.function.metadata.js";
import "core-js/modules/esnext.map.delete-all.js";
import "core-js/modules/esnext.map.emplace.js";
import "core-js/modules/esnext.map.every.js";
import "core-js/modules/esnext.map.filter.js";
import "core-js/modules/esnext.map.find.js";
import "core-js/modules/esnext.map.find-key.js";
import "core-js/modules/esnext.map.includes.js";
import "core-js/modules/esnext.map.key-of.js";
import "core-js/modules/esnext.map.map-keys.js";
import "core-js/modules/esnext.map.map-values.js";
import "core-js/modules/esnext.map.merge.js";
import "core-js/modules/esnext.map.reduce.js";
import "core-js/modules/esnext.map.some.js";
import "core-js/modules/esnext.map.update.js";
import "core-js/modules/esnext.symbol.metadata.js";
let _initProto, _setBackgroundColorDecs, _setToolTypeDecs, _setStrokeWidthDecs, _setStrokeColorDecs, _setTextColorDecs, _setTextSizeDecs, _insertImageDecs, _setContainerSizeRatioDecs, _setBoardTransparentDecs, _updateWindowSizeDecs, _setAutoCancelDrawDecs, _handleApplicationLaunchDecs, _handleApplicationTerminalDecs, _enableAutoCancelDecs;
function _applyDecs(e, t, r, n, o, a) { function i(e, t, r) { return function (n, o) { return r && r(n), e[t].call(n, o); }; } function c(e, t) { for (var r = 0; r < e.length; r++) e[r].call(t); return t; } function s(e, t, r, n) { if ("function" != typeof e && (n || void 0 !== e)) throw new TypeError(t + " must " + (r || "be") + " a function" + (n ? "" : " or undefined")); return e; } function applyDec(e, t, r, n, o, a, c, u, l, f, p, d, h) { function m(e) { if (!h(e)) throw new TypeError("Attempted to access private element on non-instance"); } var y, v = t[0], g = t[3], b = !u; if (!b) { r || Array.isArray(v) || (v = [v]); var w = {}, S = [], A = 3 === o ? "get" : 4 === o || d ? "set" : "value"; f ? (p || d ? w = { get: _setFunctionName(function () { return g(this); }, n, "get"), set: function (e) { t[4](this, e); } } : w[A] = g, p || _setFunctionName(w[A], n, 2 === o ? "" : A)) : p || (w = Object.getOwnPropertyDescriptor(e, n)); } for (var P = e, j = v.length - 1; j >= 0; j -= r ? 2 : 1) { var D = v[j], E = r ? v[j - 1] : void 0, I = {}, O = { kind: ["field", "accessor", "method", "getter", "setter", "class"][o], name: n, metadata: a, addInitializer: function (e, t) { if (e.v) throw Error("attempted to call addInitializer after decoration was finished"); s(t, "An initializer", "be", !0), c.push(t); }.bind(null, I) }; try { if (b) (y = s(D.call(E, P, O), "class decorators", "return")) && (P = y);else { var k, F; O.static = l, O.private = f, f ? 2 === o ? k = function (e) { return m(e), w.value; } : (o < 4 && (k = i(w, "get", m)), 3 !== o && (F = i(w, "set", m))) : (k = function (e) { return e[n]; }, (o < 2 || 4 === o) && (F = function (e, t) { e[n] = t; })); var N = O.access = { has: f ? h.bind() : function (e) { return n in e; } }; if (k && (N.get = k), F && (N.set = F), P = D.call(E, d ? { get: w.get, set: w.set } : w[A], O), d) { if ("object" == typeof P && P) (y = s(P.get, "accessor.get")) && (w.get = y), (y = s(P.set, "accessor.set")) && (w.set = y), (y = s(P.init, "accessor.init")) && S.push(y);else if (void 0 !== P) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0"); } else s(P, (p ? "field" : "method") + " decorators", "return") && (p ? S.push(P) : w[A] = P); } } finally { I.v = !0; } } return (p || d) && u.push(function (e, t) { for (var r = S.length - 1; r >= 0; r--) t = S[r].call(e, t); return t; }), p || b || (f ? d ? u.push(i(w, "get"), i(w, "set")) : u.push(2 === o ? w[A] : i.call.bind(w[A])) : Object.defineProperty(e, n, w)), P; } function u(e, t) { return Object.defineProperty(e, Symbol.metadata || Symbol.for("Symbol.metadata"), { configurable: !0, enumerable: !0, value: t }); } if (arguments.length >= 6) var l = a[Symbol.metadata || Symbol.for("Symbol.metadata")]; var f = Object.create(null == l ? null : l), p = function (e, t, r, n) { var o, a, i = [], s = function (t) { return _checkInRHS(t) === e; }, u = new Map(); function l(e) { e && i.push(c.bind(null, e)); } for (var f = 0; f < t.length; f++) { var p = t[f]; if (Array.isArray(p)) { var d = p[1], h = p[2], m = p.length > 3, y = 16 & d, v = !!(8 & d), g = 0 == (d &= 7), b = h + "/" + v; if (!g && !m) { var w = u.get(b); if (!0 === w || 3 === w && 4 !== d || 4 === w && 3 !== d) throw Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + h); u.set(b, !(d > 2) || d); } applyDec(v ? e : e.prototype, p, y, m ? "#" + h : _toPropertyKey(h), d, n, v ? a = a || [] : o = o || [], i, v, m, g, 1 === d, v && m ? s : r); } } return l(o), l(a), i; }(e, t, o, f); return r.length || u(e, f), { e: p, get c() { var t = []; return r.length && [u(applyDec(e, [r], n, e.name, 5, f, t), f), c.bind(null, t, e)]; } }; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _setFunctionName(e, t, n) { "symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : ""); try { Object.defineProperty(e, "name", { configurable: !0, value: n ? n + " " + t : t }); } catch (e) {} return e; }
function _checkInRHS(e) { if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null")); return e; }
import { AgoraObservable } from 'agora-foundation/lib/utilities/observable';
import { convertToForgeBoardTool } from '../utils';
import { createLogger, generateLogObserver } from '../../../utilities/logger';
import { trace } from 'agora-foundation/lib/decorator/log';
import { stringSchema } from 'agora-foundation/lib/utilities/schema';
import validateParams from '../../../utilities/validate-params';
import { booleanSchema, colorSchema, fcrBoardToolTypeSchema, fcrSizeSchema, numberSchema, z } from '../../../schema';
import { bound } from 'agora-foundation/lib/decorator';
import { FcrReturnCode } from '../../../type';
import { handleRequestError } from '../../../utilities/error';
import { ErrorModuleCode } from '../../../imports';
export class FcrBoardMainWindowImpl {
static {
[_initProto] = _applyDecs(this, [[trace, 2, "addPage"], [trace, 2, "removePage"], [trace, 2, "undo"], [trace, 2, "redo"], [trace, 2, "clean"], [trace, 2, "getSnapshotImage"], [_setBackgroundColorDecs, 2, "setBackgroundColor"], [trace, 2, "getPageInfo"], [trace, 2, "prevPage"], [trace, 2, "nextPage"], [_setToolTypeDecs, 2, "setToolType"], [_setStrokeWidthDecs, 2, "setStrokeWidth"], [_setStrokeColorDecs, 2, "setStrokeColor"], [_setTextColorDecs, 2, "setTextColor"], [_setTextSizeDecs, 2, "setTextSize"], [_insertImageDecs, 2, "insertImage"], [trace, 2, "getContentView"], [_setContainerSizeRatioDecs, 2, "setContainerSizeRatio"], [_setBoardTransparentDecs, 2, "setBoardTransparent"], [_updateWindowSizeDecs, 2, "updateWindowSize"], [_setAutoCancelDrawDecs, 2, "setAutoCancelDraw"], [trace, 2, "getWritable"], [_handleApplicationLaunchDecs, 2, "_handleApplicationLaunch"], [_handleApplicationTerminalDecs, 2, "_handleApplicationTerminal"], [_enableAutoCancelDecs, 2, "_enableAutoCancel"]], []).e;
}
[(_setBackgroundColorDecs = [trace(['color']), validateParams(stringSchema)], _setToolTypeDecs = [trace(['type']), validateParams(fcrBoardToolTypeSchema)], _setStrokeWidthDecs = [trace(['strokeWidth']), validateParams(numberSchema)], _setStrokeColorDecs = [trace(['color']), validateParams(z.union([colorSchema, stringSchema]))], _setTextColorDecs = [trace(['color']), validateParams(colorSchema)], _setTextSizeDecs = [trace(['textSize']), validateParams(numberSchema)], _insertImageDecs = [trace(['resourceUrl', 'x', 'y', 'width', 'height']), validateParams(stringSchema, numberSchema, numberSchema, numberSchema, numberSchema)], _setContainerSizeRatioDecs = [trace(['ratio']), validateParams(numberSchema)], _setBoardTransparentDecs = [trace(['isTransparent']), validateParams(booleanSchema)], _updateWindowSizeDecs = [trace(['size']), validateParams(fcrSizeSchema)], _setAutoCancelDrawDecs = [trace(['enable']), validateParams(booleanSchema)], _handleApplicationLaunchDecs = [bound, trace(['appId', 'app'])], _handleApplicationTerminalDecs = [bound, trace(['appId', 'app'])], _enableAutoCancelDecs = trace(['enable']), "logger")] = (_initProto(this), createLogger({
prefix: 'FcrBoardMainWindowImpl'
}));
_observable = new AgoraObservable();
_currentPage = 0;
_background = 'rgba(0, 0, 0, 0)';
_isAutoCancelEnabled = false;
_isWritable = false;
constructor(whiteboard, boardRoom, appId, _roomId, _userId, _api) {
this._roomId = _roomId;
this._userId = _userId;
this._api = _api;
this._addLogObserver();
this._boardView = document.createElement('div');
this._boardView.classList.add('fcr-whiteboard-window-view');
this._boardView.appendChild(whiteboard.view);
this._boardView.style.height = `100%`;
this._boardView.style.width = `100%`;
this._boardView.setAttribute('data-app-id', appId);
this._whiteboard = whiteboard;
this._boardRoom = boardRoom;
this._setBoardViewStyle(whiteboard.view);
this._addWindowManagerEventListeners();
boardRoom.applicationManager.on('launch', this._handleApplicationLaunch);
boardRoom.applicationManager.on('terminal', this._handleApplicationTerminal);
// @ts-ignore
this.logger.info(`initialized, room: ${boardRoom.roomId}`);
}
internalSetWritable(isWritable) {
this.logger.info(`internalSetWritable called, isWritable: ${isWritable}, previous state: ${this._isWritable}`);
this._boardRoom.setWritable(isWritable);
this._isWritable = isWritable;
this._observable.notifyObservers('onWritable', isWritable);
this.logger.info(`Writable state updated to ${isWritable}`);
}
async setWritable(isWritable) {
this.logger.info(`setWritable called, isWritable: ${isWritable}, current state: ${this._isWritable}`);
if (!this._api) {
// api deops not exist in sub process, directly set writable state
this.internalSetWritable(isWritable);
return FcrReturnCode.SUCCESS;
}
await handleRequestError(() => this._api.setWhiteboardWriteCount({
roomId: this._roomId,
userId: this._userId,
state: isWritable ? 1 : 0
}), ErrorModuleCode.FCR_ROOM_WHITEBOARD, `set whiteboard write permission to ${isWritable} failed`);
this.logger.info(`Set writable to ${isWritable} successfully`);
this.internalSetWritable(isWritable);
return FcrReturnCode.SUCCESS;
}
async addPage() {
this._whiteboard.addPage('next');
return FcrReturnCode.SUCCESS;
}
async removePage() {
this._whiteboard.deletePage('current');
return FcrReturnCode.SUCCESS;
}
async undo() {
this._whiteboard.undo();
return FcrReturnCode.SUCCESS;
}
async redo() {
this._whiteboard.redo();
return FcrReturnCode.SUCCESS;
}
async clean() {
this._whiteboard.clearPage();
return FcrReturnCode.SUCCESS;
}
async getSnapshotImage() {
if (this._whiteboard) {
//@ts-ignore
const base64 = await this._whiteboard.rasterizeElementsBounds(this._whiteboard.view.clientWidth, 2000 // 跟惠东商定的最大高度值
);
return new Promise((resolve, reject) => {
if (base64) {
const img = new Image();
img.src = base64;
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ffffff'; // 产品确认使用白色作为导出的背景色
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
resolve(imageData);
};
} else {
reject(new Error('Failed to load image'));
}
});
} else {
throw new Error('Whiteboard is not initialized');
}
}
async setBackgroundColor(color) {
this._background = color;
this.logger.info(`set canvas background color: ${this._background}`);
this._whiteboard.setCanvasBackgroundColor(color);
return FcrReturnCode.SUCCESS;
}
getPageInfo() {
return {
showIndex: this._currentPage,
count: this._whiteboard.pageList().length || 0
};
}
async prevPage() {
this._whiteboard.indexedNavigation.prevPage();
return FcrReturnCode.SUCCESS;
}
async nextPage() {
this._whiteboard.indexedNavigation.nextPage();
return FcrReturnCode.SUCCESS;
}
async setToolType(type) {
if (this._whiteboard) {
const transType = convertToForgeBoardTool(type);
this._whiteboard.setCurrentTool(transType);
} else {
throw new Error('Whiteboard is not initialized');
}
return FcrReturnCode.SUCCESS;
}
async setStrokeWidth(strokeWidth) {
if (this._whiteboard) {
this._whiteboard.strokeWidth = strokeWidth;
} else {
throw new Error('Whiteboard is not initialized');
}
return FcrReturnCode.SUCCESS;
}
async setStrokeColor(color) {
const c = typeof color === 'string' ? color : `rgb(${color.r},${color.g},${color.b})`;
if (this._whiteboard) {
this._whiteboard.strokeColor = c;
} else {
throw new Error('Whiteboard is not initialized');
}
return FcrReturnCode.SUCCESS;
}
async setTextColor(color) {
const c = typeof color === 'string' ? color : `rgb(${color.r},${color.g},${color.b})`;
if (this._whiteboard) {
this._whiteboard.fillColor = c;
} else {
throw new Error('Whiteboard is not initialized');
}
return FcrReturnCode.SUCCESS;
}
async setTextSize(textSize) {
if (this._whiteboard) {
this._whiteboard.fontSize = textSize;
} else {
throw new Error('Whiteboard is not initialized');
}
return FcrReturnCode.SUCCESS;
}
async insertImage(resourceUrl, x, y, width, height) {}
getContentView() {
return this._boardView;
}
async setContainerSizeRatio(ratio) {}
async setBoardTransparent(isTransparent) {}
updateWindowSize(size) {
size && this._whiteboard.updateViewport(size.width, size.height);
return FcrReturnCode.SUCCESS;
}
setAutoCancelDraw(enable) {
this._isAutoCancelEnabled = enable;
this._enableAutoCancel(enable);
return FcrReturnCode.SUCCESS;
}
getWritable() {
return this._isWritable;
}
addObserver(observer) {
this._observable.addObserver(observer);
}
removeObserver(observer) {
this._observable.removeObserver(observer);
}
_addWindowManagerEventListeners() {
const windowManager = this._whiteboard;
windowManager.indexedNavigation.on('activePageChange', showIndex => {
this._currentPage = showIndex;
const state = {
showIndex: showIndex,
count: windowManager.indexedNavigation.pageCount || 0
};
this._observable.notifyObservers('onPageInfoUpdated', state);
});
windowManager.indexedNavigation.on('pageCountChange', count => {
const state = {
showIndex: this._currentPage,
count
};
this._observable.notifyObservers('onPageInfoUpdated', state);
});
windowManager.on('undoStackLength', steps => {
this._observable.notifyObservers('onUndoStateUpdated', steps > 0);
});
windowManager.on('redoStackLength', steps => {
this._observable.notifyObservers('onRedoStateUpdated', steps > 0);
});
}
_handleApplicationLaunch(appId, app) {
this._whiteboard = app;
this._setBoardViewStyle(app.view);
if (this._background) {
this.logger.info(`set canvas background color: ${this._background}`);
this._whiteboard.setCanvasBackgroundColor(this._background);
}
this._enableAutoCancel(this._isAutoCancelEnabled);
if (this._boardView.hasChildNodes()) {
this.logger.warn('board view is not empty, clear board view before appending new view');
this._boardView.innerHTML = '';
}
this._boardView.setAttribute('data-app-id', appId);
this._boardView.appendChild(app.view);
return FcrReturnCode.SUCCESS;
}
_handleApplicationTerminal(appId, app) {
try {
if (this._boardView.contains(app.view)) {
this.logger.info(`remove application view: ${appId}`);
this._boardView.removeChild(app.view);
} else {
this.logger.warn(`application view not found in board view: ${appId}`);
}
} catch (e) {
this.logger.error(`failed to handle terminal: ${appId}`, e);
}
return FcrReturnCode.SUCCESS;
}
_setBoardViewStyle(view) {
view.style.height = `100%`;
view.style.width = `100%`;
view.style.backgroundColor = 'rgba(0, 0, 0, 0)';
}
_enableAutoCancel(enable) {
// @ts-ignore
this._whiteboard.__setMainCanvasVisible(!enable);
// @ts-ignore
this._whiteboard.__delayTranslateOut = enable ? 4000 : -1;
return FcrReturnCode.SUCCESS;
}
_addLogObserver() {
this.addObserver(generateLogObserver(this.logger, [['onWritable', ['isWritable']], ['onPageInfoUpdated', ['info']], ['onUndoStateUpdated', ['enable']], ['onRedoStateUpdated', ['enable']]]));
}
}