Skip to content

Commit 81a8f69

Browse files
committed
feat(PageTabs): add functionality to create new pages with an "Add Page" button
1 parent 1ab6880 commit 81a8f69

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

src/components/ContentWrap.jsx

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,13 @@ export default class ContentWrap extends Component {
647647
const baseTranspilerPath = 'lib/transpilers';
648648
// Exit if already loaded
649649
var d = deferred();
650+
651+
// Add null check for modes[mode]
652+
if (!mode || !modes[mode]) {
653+
d.resolve();
654+
return d.promise;
655+
}
656+
650657
if (modes[mode].hasLoaded) {
651658
d.resolve();
652659
return d.promise;
@@ -688,36 +695,57 @@ export default class ContentWrap extends Component {
688695
updateHtmlMode(value) {
689696
this.props.onCodeModeChange('html', value);
690697
this.props.currentItem.htmlMode = value;
691-
CodeMirror.autoLoadMode(
692-
this.cm.html,
693-
modes[value].cmPath || modes[value].cmMode,
694-
);
698+
699+
// Add null check to prevent "Cannot read properties of undefined (reading 'cmPath')" error
700+
if (this.cm && this.cm.html && modes[value]) {
701+
CodeMirror.autoLoadMode(
702+
this.cm.html,
703+
modes[value].cmPath || modes[value].cmMode,
704+
);
705+
}
706+
695707
return this.handleModeRequirements(value);
696708
}
697709

698710
updateCssMode(value) {
699711
this.props.onCodeModeChange('css', value);
700712
this.props.currentItem.cssMode = value;
701-
this.cm.css.setOption('mode', modes[value].cmMode);
702-
this.cm.css.setOption('readOnly', modes[value].cmDisable);
703-
window.cssSettingsBtn.classList[
704-
modes[value].hasSettings ? 'remove' : 'add'
705-
]('hide');
706-
CodeMirror.autoLoadMode(
707-
this.cm.css,
708-
modes[value].cmPath || modes[value].cmMode,
709-
);
713+
714+
// Add null check to prevent "Cannot read properties of undefined" error
715+
if (this.cm && this.cm.css && modes[value]) {
716+
this.cm.css.setOption('mode', modes[value].cmMode);
717+
this.cm.css.setOption('readOnly', modes[value].cmDisable);
718+
719+
CodeMirror.autoLoadMode(
720+
this.cm.css,
721+
modes[value].cmPath || modes[value].cmMode,
722+
);
723+
}
724+
725+
// Only modify DOM if the element exists
726+
if (window.cssSettingsBtn && modes[value]) {
727+
window.cssSettingsBtn.classList[
728+
modes[value].hasSettings ? 'remove' : 'add'
729+
]('hide');
730+
}
731+
710732
return this.handleModeRequirements(value);
711733
}
712734

713735
updateJsMode(value) {
714736
this.props.onCodeModeChange('js', value);
715737
this.props.currentItem.jsMode = value;
716-
this.cm.js.setOption('mode', modes[value].cmMode);
717-
CodeMirror.autoLoadMode(
718-
this.cm.js,
719-
modes[value].cmPath || modes[value].cmMode,
720-
);
738+
739+
// Add null check to prevent "Cannot read properties of undefined" error
740+
if (this.cm && this.cm.js && modes[value]) {
741+
this.cm.js.setOption('mode', modes[value].cmMode);
742+
743+
CodeMirror.autoLoadMode(
744+
this.cm.js,
745+
modes[value].cmPath || modes[value].cmMode,
746+
);
747+
}
748+
721749
return this.handleModeRequirements(value);
722750
}
723751

@@ -988,17 +1016,15 @@ export default class ContentWrap extends Component {
9881016
<UserCodeMirror
9891017
ref={(dslEditor) => (this.dslEditor = dslEditor)}
9901018
options={{
991-
mode: 'htmlmixed',
992-
profile: 'xhtml',
1019+
mode: 'javascript',
9931020
gutters: [
9941021
'CodeMirror-linenumbers',
9951022
'CodeMirror-foldgutter',
9961023
],
9971024
noAutocomplete: true,
998-
matchTags: { bothTags: true },
9991025
prettier: true,
1000-
prettierParser: 'html',
1001-
emmet: true,
1026+
prettierParser: 'babel',
1027+
emmet: false,
10021028
}}
10031029
prefs={this.props.prefs}
10041030
autoComplete={this.props.prefs.autoComplete}

src/components/app.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,10 +1516,24 @@ BookLibService.Borrow(id) {
15161516
return currentItem.pages.find(page => page.id === currentItem.currentPageId) || null;
15171517
}
15181518

1519-
addNewPage(title = 'New Page') {
1519+
addNewPage(title) {
15201520
const { currentItem } = this.state;
15211521
if (!currentItem) return null;
15221522

1523+
// Ensure pages array exists
1524+
if (!currentItem.pages || !Array.isArray(currentItem.pages)) {
1525+
// Migrate the item to the pages format if needed
1526+
const migratedItem = migrateItemToPages(currentItem);
1527+
this.setState({ currentItem: migratedItem });
1528+
return this.addNewPage(title); // Retry after migration
1529+
}
1530+
1531+
// If no title is provided, generate one based on the number of pages
1532+
if (!title) {
1533+
const pageCount = currentItem.pages.length + 1;
1534+
title = `Page ${pageCount}`;
1535+
}
1536+
15231537
const newPage = {
15241538
id: generateRandomId(),
15251539
title,

0 commit comments

Comments
 (0)