Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EMBED Ts observable changes #8330

Merged
merged 41 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e747516
observable
ShaMan123 Jul 31, 2022
18531c1
cleanup
ShaMan123 Jul 31, 2022
058dfef
Update observable.js
ShaMan123 Jul 31, 2022
6dcd9fe
Update index.js
ShaMan123 Jul 31, 2022
9c49608
Update Observable.ts
ShaMan123 Jul 31, 2022
cd758d7
watch abort build on change
ShaMan123 Aug 1, 2022
ab7acec
finally it works
ShaMan123 Aug 1, 2022
2189e03
Update text.class.js
ShaMan123 Aug 1, 2022
45c5447
Update Observable.ts
ShaMan123 Aug 1, 2022
19eb2f9
Update index.js
ShaMan123 Aug 1, 2022
42a56a3
m
ShaMan123 Aug 1, 2022
8837320
Update src/util/lang_object.js
ShaMan123 Aug 1, 2022
b4dba4d
Merge branch 'master' into ts-observable2
ShaMan123 Aug 8, 2022
7cacd09
Update Observable.ts
ShaMan123 Aug 8, 2022
2e47a84
revert naming
ShaMan123 Aug 8, 2022
83d0d2c
revert naming
ShaMan123 Aug 8, 2022
cec3a29
fix!
ShaMan123 Aug 8, 2022
40c3eb2
hack proto
ShaMan123 Aug 8, 2022
6597aea
Update static_canvas.class.ts
ShaMan123 Aug 8, 2022
3190b51
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
94ef263
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
718be07
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
c09807d
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
2273dc8
Update observable.js
ShaMan123 Aug 8, 2022
9720ca5
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
4d150b7
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
1ee0305
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
2fa86e3
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
e3240da
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
576b528
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
aa81507
Update observable.mixin.ts
ShaMan123 Aug 8, 2022
09dbf30
Merge branch 'master' into ts-observable2
ShaMan123 Aug 17, 2022
0d0f2d1
Merge branch 'master' into ts-observable2
ShaMan123 Aug 25, 2022
65b7f89
Update static_canvas.class.ts
ShaMan123 Aug 25, 2022
289e081
Merge branch 'master' into ts-observable2
ShaMan123 Aug 26, 2022
e65f929
Merge branch 'master' into ts-observable2
ShaMan123 Aug 29, 2022
996e28d
Merge branch 'master' into ts-observable2
ShaMan123 Aug 30, 2022
f7afef8
Merge branch 'master' into ts-observable2
ShaMan123 Sep 4, 2022
d5a8a2a
Merge branch 'master' into ts-observable2
ShaMan123 Sep 14, 2022
16b1e02
Update observable.mixin.ts
ShaMan123 Sep 14, 2022
93e54d4
Merge branch 'converting-object-to-class' into ts-observable2
asturur Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 84 additions & 84 deletions src/mixins/observable.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,145 +1,145 @@
//@ts-nocheck
(function (global) {
var fabric = global.fabric;

/**
* @private
* @param {String} eventName
* @param {Function} handler
*/
function _removeEventListener(eventName, handler) {
if (!this.__eventListeners[eventName]) {
return;
}
var eventListener = this.__eventListeners[eventName];
if (handler) {
eventListener[eventListener.indexOf(handler)] = false;
} else {
eventListener.fill(false);
}
}
import { fabric } from '../../HEADER';

type EventRegistryObject = Record<string, Function>;

/**
* @tutorial {@link http://fabricjs.com/fabric-intro-part-2#events}
* @see {@link http://fabricjs.com/events|Events demo}
*/
export class Observable {
private __eventListeners: Record<Function[]> = {};

/**
* Observes specified event
* @memberOf fabric.Observable
* @alias on
* @param {String|Object} eventName Event name (eg. 'after:render') or object with key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {string} eventName Event name (eg. 'after:render')
* @param {EventRegistryObject} handlers key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {Function} handler Function that receives a notification when an event of the specified type occurs
* @return {Function} disposer
*/
function on(eventName, handler) {
on(eventName: string, handler: Function): Function;
on(handlers: EventRegistryObject): Function;
on(arg0: string | EventRegistryObject, handler?: Function): Function {
if (!this.__eventListeners) {
this.__eventListeners = {};
}
// one object with key/value pairs was passed
if (arguments.length === 1) {
for (var prop in eventName) {
this.on(prop, eventName[prop]);
if (typeof arg0 === 'object') {
// one object with key/value pairs was passed
for (const eventName in arg0) {
this.on(eventName, arg0[eventName]);
}
} else {
return () => this.off(arg0);
} else if (handler) {
const eventName = arg0;
if (!this.__eventListeners[eventName]) {
this.__eventListeners[eventName] = [];
}
this.__eventListeners[eventName].push(handler);
return () => this.off(eventName, handler);
} else {
// noop
return () => false;
}
return off.bind(this, eventName, handler);
}

function _once(eventName, handler) {
var _handler = function () {
handler.apply(this, arguments);
this.off(eventName, _handler);
}.bind(this);
this.on(eventName, _handler);
return _handler;
}

/**
* Observes specified event **once**
* @memberOf fabric.Observable
* @alias once
* @param {String|Object} eventName Event name (eg. 'after:render') or object with key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {string} eventName Event name (eg. 'after:render')
* @param {EventRegistryObject} handlers key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {Function} handler Function that receives a notification when an event of the specified type occurs
* @return {Function} disposer
*/
function once(eventName, handler) {
// one object with key/value pairs was passed
if (arguments.length === 1) {
var handlers = {};
for (var prop in eventName) {
handlers[prop] = _once.call(this, prop, eventName[prop]);
once(eventName: string, handler: Function): Function;
once(handlers: EventRegistryObject): Function;
once(arg0: string | EventRegistryObject, handler?: Function): Function {
if (typeof arg0 === 'object') {
// one object with key/value pairs was passed
const disposers: Function[] = [];
for (const eventName in arg0) {
disposers.push(this.once(eventName, arg0[eventName]));
}
return off.bind(this, handlers);
return () => disposers.forEach((d) => d());
} else if (handler) {
const disposer = this.on(arg0, (...args: any[]) => {
handler(...args);
disposer();
});
return disposer;
} else {
var _handler = _once.call(this, eventName, handler);
return off.bind(this, eventName, _handler);
// noop
return () => false;
}
}

/**
* @private
* @param {string} eventName
* @param {Function} [handler]
*/
private _removeEventListener(eventName: string, handler?: Function) {
if (!this.__eventListeners[eventName]) {
return;
}

if (handler) {
const eventListener = this.__eventListeners[eventName];
const index = eventListener.indexOf(handler);
index > -1 && eventListener.splice(index, 1);
} else {
this.__eventListeners[eventName] = [];
}
}

/**
* Stops event observing for a particular event handler. Calling this method
* without arguments removes all handlers for all events
* @memberOf fabric.Observable
* @alias off
* @param {String|Object} eventName Event name (eg. 'after:render') or object with key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {string} eventName Event name (eg. 'after:render')
* @param {EventRegistryObject} handlers key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {Function} handler Function to be deleted from EventListeners
*/
function off(eventName, handler) {
off(eventName: string, handler: Function): void;
off(handlers: EventRegistryObject): void;
off(arg0?: string | EventRegistryObject, handler?: Function) {
if (!this.__eventListeners) {
return;
}

// remove all key/value pairs (event name -> event handler)
if (arguments.length === 0) {
for (eventName in this.__eventListeners) {
_removeEventListener.call(this, eventName);
if (typeof arg0 === 'undefined') {
for (const eventName in this.__eventListeners) {
this._removeEventListener(eventName);
}
}
// one object with key/value pairs was passed
else if (typeof eventName === 'object' && typeof handler === 'undefined') {
for (var prop in eventName) {
_removeEventListener.call(this, prop, eventName[prop]);
else if (typeof arg0 === 'object') {
for (const eventName in arg0) {
this._removeEventListener(eventName, arg0[eventName]);
}
} else {
_removeEventListener.call(this, eventName, handler);
this._removeEventListener(arg0, handler);
}
}

/**
* Fires event with an optional options object
* @memberOf fabric.Observable
* @param {String} eventName Event name to fire
* @param {Object} [options] Options object
*/
function fire(eventName, options) {
fire(eventName: string, options: object) {
if (!this.__eventListeners) {
return;
}

var listenersForEvent = this.__eventListeners[eventName];
if (!listenersForEvent) {
return;
}

for (var i = 0, len = listenersForEvent.length; i < len; i++) {
listenersForEvent[i] && listenersForEvent[i].call(this, options || {});
const listenersForEvent = this.__eventListeners[eventName]?.concat();
if (listenersForEvent) {
for (let i = 0; i < listenersForEvent.length; i++) {
listenersForEvent[i].call(this, options || {});
}
}
this.__eventListeners[eventName] = listenersForEvent.filter(function (
value
) {
return value !== false;
});
}
}

/**
* @namespace fabric.Observable
* @tutorial {@link http://fabricjs.com/fabric-intro-part-2#events}
* @see {@link http://fabricjs.com/events|Events demo}
*/
fabric.Observable = {
fire: fire,
on: on,
once: once,
off: off,
};
})(typeof exports !== 'undefined' ? exports : window);
fabric.Observable = Observable;
9 changes: 0 additions & 9 deletions src/shapes/itext.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { FabricObject } from './object.class';
* prefix when observing canvas.
* @class fabric.IText
* @extends fabric.Text
* @mixes fabric.Observable
*
* @fires changed
* @fires selection:changed
Expand Down Expand Up @@ -57,15 +56,7 @@ import { FabricObject } from './object.class';
*/
fabric.IText = fabric.util.createClass(
fabric.Text,
fabric.Observable,
/** @lends fabric.IText.prototype */ {
/**
* Type of an object
* @type String
* @default
*/
type: 'i-text',

/**
* Index where text selection starts (or where cursor is when there is no selection)
* @type Number
Expand Down
1 change: 1 addition & 0 deletions src/shapes/object.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fabric } from '../../HEADER';
import { cache } from '../cache';
import { config } from '../config';
import { VERSION } from '../constants';
import { Observable } from '../mixins/observable.mixin';
import { Point } from '../point.class';
import { capValue } from '../util/misc/capValue';
import { pick } from '../util/misc/pick';
Expand Down
2 changes: 0 additions & 2 deletions src/shapes/text.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,4 @@ import { DEFAULT_SVG_FONT_SIZE } from '../constants';
'fantasy',
'monospace',
];

fabric.util.createAccessors && fabric.util.createAccessors(fabric.Text);
})(typeof exports !== 'undefined' ? exports : window);
3 changes: 1 addition & 2 deletions src/shapes/textbox.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
* wrapping of lines.
* @class fabric.Textbox
* @extends fabric.IText
* @mixes fabric.Observable
* @return {fabric.Textbox} thisArg
* @see {@link fabric.Textbox#initialize} for constructor definition
*/
fabric.Textbox = fabric.util.createClass(fabric.IText, fabric.Observable, {
fabric.Textbox = fabric.util.createClass(fabric.IText, {
/**
* Type of an object
* @type String
Expand Down
11 changes: 10 additions & 1 deletion src/static_canvas.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ts-nocheck
import { config } from './config';
import { VERSION } from './constants';
import { Observable } from './mixins/observable.mixin';
import { Point } from './point.class';
import { requestAnimFrame } from './util/animate';
import { removeFromArray } from './util/internals';
Expand Down Expand Up @@ -1853,7 +1854,15 @@ import { FabricObject } from './shapes/object.class';
}
);

extend(fabric.StaticCanvas.prototype, fabric.Observable);
// hack - class methods are not enumrable
// TODO remove when migrating to es6
Object.getOwnPropertyNames(Observable.prototype).forEach(key => {
if (key === 'constructor') return;
Object.defineProperty(fabric.StaticCanvas.prototype, key, {
value: Observable.prototype[key]
});
});

extend(fabric.StaticCanvas.prototype, fabric.DataURLExporter);

extend(
Expand Down
Loading