Skip to content

Commit ac5d1cd

Browse files
committed
Try to support prev tab plugin
1 parent 1fd4804 commit ac5d1cd

File tree

7 files changed

+25
-12
lines changed

7 files changed

+25
-12
lines changed

dist/grapesjs-tabs.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/grapesjs-tabs.min.js.map

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

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "grapesjs-tabs",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Simple tabs component plugin for GrapesJS",
55
"main": "dist/grapesjs-tabs.min.js",
66
"scripts": {

src/components/Tab.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const role = 'tab';
33
export default (dc, {
44
defaultModel, typeTabs, selectorTab, editor, ...config
55
}) => {
6+
const prvSel = config.selectorTabPrev;
67
const traits = [
78
{
89
full: 1,
@@ -68,7 +69,8 @@ export default (dc, {
6869
},
6970

7071
getTabContent() {
71-
const id = this.getAttributes()[selectorTab];
72+
const attrs = this.getAttributes();
73+
const id = attrs[prvSel] || attrs[selectorTab];
7274
const tabs = this.getTabsType();
7375
if (!tabs || !id) return;
7476
const contents = tabs.findContents();

src/components/Tabs.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default (dc, {
77
const el = this;
88
const classTabActive = props.classactive;
99
const selectorTab = props.selectortab;
10+
const prevsel = props.prevsel;
1011
const { history, _isEditor } = window;
1112
const attrTabindex = 'tabIndex';
1213
const attrSelected = 'ariaSelected';
@@ -24,6 +25,11 @@ export default (dc, {
2425
each(el.querySelectorAll(roleTabContent), i => i.hidden = true);
2526
}
2627

28+
const getTabId = (item) => {
29+
return (prevsel && item.getAttribute(prevsel)) || item.getAttribute(selectorTab);
30+
}
31+
32+
const qS = (elem, qs) => elem.querySelector(qs);
2733
const getAllTabs = () => el.querySelectorAll(roleTab);
2834
const upTabIdx = (item, val) => !_isEditor && (item[attrTabindex] = val);
2935

@@ -37,14 +43,15 @@ export default (dc, {
3743
tabEl.className += ' ' + classTabActive;
3844
tabEl[attrSelected] = 'true';
3945
upTabIdx(tabEl, '0');
40-
const tabContentId = tabEl.getAttribute(selectorTab);
41-
const tabContent = tabContentId && el.querySelector(`#${tabContentId}`);
46+
const tabContentId = getTabId(tabEl);
47+
const tabContent = tabContentId && qS(el, `#${tabContentId}`);
4248
tabContent && (tabContent.hidden = false);
4349
};
4450

4551
const getTabByHash = () => {
4652
const hashId = (location.hash || '').replace('#', '');
47-
return hashId && el.querySelector(`${roleTab}[${selectorTab}=${hashId}]`);
53+
const qrStr = att => `${roleTab}[${att}=${hashId}]`;
54+
return hashId && (qS(el, qrStr(prevsel)) || qS(el, qrStr(selectorTab)));
4855
};
4956

5057
const getSelectedTab = (target) => {
@@ -56,8 +63,8 @@ export default (dc, {
5663
return found;
5764
};
5865

59-
let tabToActive = el.querySelector(`.${classTabActive}${roleTab}`);
60-
tabToActive = tabToActive || getTabByHash() || el.querySelector(roleTab);
66+
let tabToActive = qS(el, `.${classTabActive}${roleTab}`);
67+
tabToActive = tabToActive || getTabByHash() || qS(el, roleTab);
6168
tabToActive && activeTab(tabToActive);
6269

6370
el.addEventListener('click', (ev) => {
@@ -73,7 +80,7 @@ export default (dc, {
7380
ev.preventDefault();
7481
ev.__trg = 1;
7582
activeTab(target);
76-
const id = target.getAttribute(selectorTab);
83+
const id = getTabId(target);
7784
try {
7885
history && history.pushState(null, null, `#${id}`);
7986
} catch (e) {}
@@ -100,6 +107,7 @@ export default (dc, {
100107
name: 'Tabs',
101108
classactive: config.classTabActive,
102109
selectortab: config.selectorTab,
110+
prevsel: config.selectorTabPrev,
103111
'script-props': ['classactive', 'selectortab'],
104112
script,
105113
traits,

src/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export default {
3636
// The attribute used inside tabs as a selector for tab contents
3737
selectorTab: 'aria-controls',
3838

39+
// Special option, used mainly to support previous plugin version
40+
selectorTabPrev: 'href',
41+
3942
// Tabs component id
4043
typeTabs: 'tabs',
4144

0 commit comments

Comments
 (0)