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

feat(editor): allow setting defaultTab in tabbed examples #758

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
34 changes: 24 additions & 10 deletions editor/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,32 @@
// show the header
header.classList.remove("hidden");

/* Initialise the editors. If there is a `dataset` property
of type `tabs` on the `editorContainer`, pass its value
to `initEditor` */
if (editorContainer.dataset && editorContainer.dataset.tabs) {
tabby.initEditor(
editorContainer.dataset.tabs.split(","),
document.getElementById("js")
);
} else {
tabby.initEditor(["html", "css"], document.getElementById("html"));
/**
* @returns {string[]} - IDs of editors that should be visible in the example.
*/
function getTabs(editorContainer) {
if(editorContainer.dataset && editorContainer.dataset.tabs)
return editorContainer.dataset.tabs.split(",");
else
return ["html", "css"];
NiedziolkaMichal marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @returns {string} - ID of editor that should be active be default.
*/
function getDefaultTab(editorContainer, tabs) {
if(editorContainer.dataset && editorContainer.dataset.defaultTab)
return editorContainer.dataset.defaultTab;
else if(tabs.includes("js"))
return "js";
caugner marked this conversation as resolved.
Show resolved Hide resolved
else
return "html";
NiedziolkaMichal marked this conversation as resolved.
Show resolved Hide resolved
}

let tabs = getTabs(editorContainer);
let defaultTab = getDefaultTab(editorContainer, tabs);
tabby.initEditor(tabs, document.getElementById(defaultTab));

mceConsole();

tabby.registerEventListeners();
Expand Down
2 changes: 1 addition & 1 deletion editor/tmpl/live-tabbed-tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<h4 class="output-heading">%title%</h4>
<button id="reset" class="reset" type="button">Reset</button>
</header>
<div id="editor-container" class="editor-container %editor-height%" %active-tabs%>
<div id="editor-container" class="editor-container %editor-height%" %active-tabs% %default-tab%>
<section id="tab-container" class="tabs">
<div class="tab-list" id="tablist" role="tablist">
<button role="tab" aria-selected="false" aria-controls="html-panel"
Expand Down
16 changes: 16 additions & 0 deletions lib/pageBuilderUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ module.exports = {
return tmpl.replace(regex, "");
}
},
/**
* Sets the tab that should be open by default.
* @param {Object} currentPage - The current page object
* @param {String} tmpl - The template as a string
*
* @returns The processed template with the default tab id
*/
setDefaultTab: function (currentPage, tmpl) {
const regex = /%default-tab%/g;

if (currentPage.defaultTab) {
return tmpl.replace(regex, `data-default-tab="${currentPage.defaultTab}"`);
} else {
return tmpl.replace(regex, "");
}
},
/**
* If the `currentPage.type` is of type webapi-tabbed,
* show the console, else hide. Also set the appropriate
Expand Down
2 changes: 2 additions & 0 deletions lib/tabbedPageBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function buildTabbedExample(tmpl, currentPage) {
tmpl = pageBuilderUtils.setEditorHeight(currentPage, tmpl);
// set the active tabs to show
tmpl = pageBuilderUtils.setActiveTabs(currentPage, tmpl);
// set the default tab to open
tmpl = pageBuilderUtils.setDefaultTab(currentPage, tmpl);
// set the console state
tmpl = pageBuilderUtils.setConsoleState(currentPage, tmpl);

Expand Down