Skip to content

Commit 107b362

Browse files
committed
improve array subscribers
1 parent 6b2b0b3 commit 107b362

File tree

9 files changed

+101
-46
lines changed

9 files changed

+101
-46
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ Changes
44
1.0 (unreleased)
55
----------------
66

7+
- Extend JS by ``tiptap_on_array_add`` and ``register_array_subscribers``
8+
functions to enable usage in ``yafowil.widget.array``.
9+
[lenadax]
10+
711
- Make it work.
812
[lenadax]

js/src/bundles/widget.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import $ from 'jquery';
22

33
import {TiptapWidget} from '../widget.js';
4+
import {register_array_subscribers} from '../widget.js';
45
export * from '../widget.js';
56

67
$(function() {
@@ -11,4 +12,5 @@ $(function() {
1112
} else {
1213
TiptapWidget.initialize();
1314
}
15+
register_array_subscribers();
1416
});

js/src/widget.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export class TiptapWidget {
66
static initialize(context) {
77
$('div.tiptap-editor', context).each(function() {
88
let elem = $(this);
9+
if (elem.parents('.arraytemplate').length) {
10+
return;
11+
}
912
new TiptapWidget(elem, {
1013
actions: elem.data('tiptap-actions'),
1114
colors: elem.data('tiptap-colors'),
@@ -146,25 +149,9 @@ function tiptap_on_array_add(inst, context) {
146149
TiptapWidget.initialize(context);
147150
}
148151

149-
function tiptap_on_array_index(inst, row, index) {
150-
$('.div.tiptap-editor', row).each(function() {
151-
let trigger = $(this),
152-
ref_name = trigger.data('reference-name'),
153-
base_id = inst.base_id(row),
154-
base_name = base_id.replace(/\-/g, '.');
155-
trigger.data('reference-name', inst.set_value_index(
156-
ref_name,
157-
base_name,
158-
index,
159-
'.'
160-
));
161-
});
162-
}
163-
164-
$(function() {
165-
if (yafowil_array === undefined) {
152+
export function register_array_subscribers() {
153+
if (window.yafowil_array === undefined) {
166154
return;
167155
}
168-
yafowil_array.on_array_event('on_add', tiptap_on_array_add);
169-
yafowil_array.on_array_event('on_index', tiptap_on_array_index);
170-
});
156+
window.yafowil_array.on_array_event('on_add', tiptap_on_array_add);
157+
}

js/tests/test_widget.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {TiptapWidget} from '../src/widget.js';
2+
import {register_array_subscribers} from '../src/widget.js';
23
import $ from 'jquery';
34

45
function create_elem() {
@@ -56,6 +57,53 @@ QUnit.module('TiptapWidget', hooks => {
5657
assert.true(widget.textarea.is('textarea', elem));
5758
});
5859

60+
QUnit.test('register_array_subscribers', assert => {
61+
$('#container').empty();
62+
63+
let _array_subscribers = {
64+
on_add: []
65+
};
66+
67+
// window.yafowil_array is undefined - return
68+
register_array_subscribers();
69+
assert.deepEqual(_array_subscribers['on_add'], []);
70+
71+
// patch yafowil_array
72+
window.yafowil_array = {
73+
on_array_event: function(evt_name, evt_function) {
74+
_array_subscribers[evt_name] = evt_function;
75+
}
76+
};
77+
register_array_subscribers();
78+
79+
// create table DOM
80+
let table = $('<table />')
81+
.append($('<tr />'))
82+
.append($('<td />'))
83+
.appendTo('body');
84+
85+
elem = create_elem();
86+
$('td', table).addClass('arraytemplate');
87+
elem.appendTo($('td', table));
88+
// set data attr
89+
elem.data('tiptap-actions', ['bold']);
90+
91+
// invoke array on_add - returns
92+
_array_subscribers['on_add'].apply(null, $('tr', table));
93+
let widget = elem.data('yafowil-tiptap');
94+
assert.notOk(widget);
95+
$('td', table).removeClass('arraytemplate');
96+
97+
// invoke array on_add
98+
elem.attr('id', '');
99+
_array_subscribers['on_add'].apply(null, $('tr', table));
100+
widget = elem.data('yafowil-tiptap');
101+
assert.ok(widget);
102+
table.remove();
103+
window.yafowil_array = undefined;
104+
_array_subscribers = undefined;
105+
});
106+
59107
QUnit.test('destroy', assert => {
60108
// set data attr
61109
elem.data('tiptap-actions', ['bold']);

package.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@
66
"devDependencies": {
77
"@rollup/plugin-node-resolve": "^13.3.0",
88
"@rollup/plugin-typescript": "^8.3.0",
9-
"karma": "^6.3.20",
9+
"karma": "^6.4.1",
1010
"karma-chrome-launcher": "^3.1.1",
1111
"karma-coverage": "^2.2.0",
1212
"karma-firefox-launcher": "^2.1.2",
1313
"karma-module-resolver-preprocessor": "^1.1.3",
1414
"karma-qunit": "^4.1.2",
1515
"karma-typescript": "^5.5.3",
1616
"karma-viewport": "^1.0.9",
17-
"qunit": "^2.19.1",
18-
"rollup": "^2.75.3",
17+
"qunit": "^2.19.3",
18+
"rollup": "^2.79.1",
1919
"rollup-plugin-cleanup": "^3.2.1",
2020
"rollup-plugin-export-default": "^1.4.0",
2121
"rollup-plugin-terser": "^7.0.2",
2222
"rollup-plugin-ts": "^2.0.5",
2323
"rollup-plugin-typescript2": "^0.31.1",
24-
"sass": "^1.52.1",
24+
"sass": "^1.57.1",
2525
"tslib": "^2.3.1"
2626
},
2727
"dependencies": {
28-
"@tiptap/core": "^2.0.0-beta.176",
29-
"@tiptap/extension-blockquote": "^2.0.0-beta.26",
30-
"@tiptap/extension-bold": "^2.0.0-beta.26",
31-
"@tiptap/extension-bullet-list": "^2.0.0-beta.26",
32-
"@tiptap/extension-code": "^2.0.0-beta.26",
33-
"@tiptap/extension-code-block": "^2.0.0-beta.37",
28+
"@tiptap/core": "^2.0.0-beta.209",
29+
"@tiptap/extension-blockquote": "^2.0.0-beta.209",
30+
"@tiptap/extension-bold": "^2.0.0-beta.209",
31+
"@tiptap/extension-bullet-list": "^2.0.0-beta.209",
32+
"@tiptap/extension-code": "^2.0.0-beta.209",
33+
"@tiptap/extension-code-block": "^2.0.0-beta.209",
3434
"@tiptap/extension-code-block-lowlight": "^2.0.0-beta.68",
35-
"@tiptap/extension-color": "^2.0.0-beta.9",
36-
"@tiptap/extension-document": "^2.0.0-beta.15",
37-
"@tiptap/extension-dropcursor": "^2.0.0-beta.25",
38-
"@tiptap/extension-heading": "^2.0.0-beta.26",
39-
"@tiptap/extension-image": "^2.0.0-beta.27",
40-
"@tiptap/extension-italic": "^2.0.0-beta.26",
41-
"@tiptap/extension-link": "^2.0.0-beta.38",
42-
"@tiptap/extension-list-item": "^2.0.0-beta.20",
43-
"@tiptap/extension-ordered-list": "^2.0.0-beta.27",
44-
"@tiptap/extension-paragraph": "^2.0.0-beta.23",
45-
"@tiptap/extension-text": "^2.0.0-beta.15",
46-
"@tiptap/extension-text-style": "^2.0.0-beta.23",
47-
"@tiptap/extension-underline": "^2.0.0-beta.23",
35+
"@tiptap/extension-color": "^2.0.0-beta.209",
36+
"@tiptap/extension-document": "^2.0.0-beta.209",
37+
"@tiptap/extension-dropcursor": "^2.0.0-beta.209",
38+
"@tiptap/extension-heading": "^2.0.0-beta.209",
39+
"@tiptap/extension-image": "^2.0.0-beta.209",
40+
"@tiptap/extension-italic": "^2.0.0-beta.209",
41+
"@tiptap/extension-link": "^2.0.0-beta.209",
42+
"@tiptap/extension-list-item": "^2.0.0-beta.209",
43+
"@tiptap/extension-ordered-list": "^2.0.0-beta.209",
44+
"@tiptap/extension-paragraph": "^2.0.0-beta.209",
45+
"@tiptap/extension-text": "^2.0.0-beta.209",
46+
"@tiptap/extension-text-style": "^2.0.0-beta.209",
47+
"@tiptap/extension-underline": "^2.0.0-beta.209",
4848
"@tiptap/starter-kit": "^2.0.0-beta.176"
4949
}
5050
}

src/yafowil/widget/tiptap/resources/tiptap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11632,8 +11632,8 @@ var tiptap = (function (exports) {
1163211632
return getText(contentNode, {
1163311633
blockSeparator,
1163411634
textSerializers: {
11635-
...textSerializers,
1163611635
...getTextSerializersFromSchema(schema),
11636+
...textSerializers,
1163711637
},
1163811638
});
1163911639
}

src/yafowil/widget/tiptap/resources/tiptap.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/yafowil/widget/tiptap/resources/widget.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,9 @@ var yafowil_tiptap = (function (exports, $) {
599599
static initialize(context) {
600600
$('div.tiptap-editor', context).each(function() {
601601
let elem = $(this);
602+
if (elem.parents('.arraytemplate').length) {
603+
return;
604+
}
602605
new TiptapWidget(elem, {
603606
actions: elem.data('tiptap-actions'),
604607
colors: elem.data('tiptap-colors'),
@@ -713,6 +716,15 @@ var yafowil_tiptap = (function (exports, $) {
713716
}
714717
}
715718
}
719+
function tiptap_on_array_add(inst, context) {
720+
TiptapWidget.initialize(context);
721+
}
722+
function register_array_subscribers() {
723+
if (window.yafowil_array === undefined) {
724+
return;
725+
}
726+
window.yafowil_array.on_array_event('on_add', tiptap_on_array_add);
727+
}
716728

717729
$(function() {
718730
if (window.ts !== undefined) {
@@ -722,9 +734,11 @@ var yafowil_tiptap = (function (exports, $) {
722734
} else {
723735
TiptapWidget.initialize();
724736
}
737+
register_array_subscribers();
725738
});
726739

727740
exports.TiptapWidget = TiptapWidget;
741+
exports.register_array_subscribers = register_array_subscribers;
728742

729743
Object.defineProperty(exports, '__esModule', { value: true });
730744

0 commit comments

Comments
 (0)