Skip to content

Commit

Permalink
Auto guess slide title from first non-blank line (impress#821)
Browse files Browse the repository at this point in the history
* Auto guess slide title from first non-blank line
* If line content is too long, trim to the first 40 bytes
  • Loading branch information
thawk authored Oct 17, 2022
1 parent 0d52faa commit b572362
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
29 changes: 28 additions & 1 deletion js/impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,33 @@
return tempDiv.firstChild;
};

var getStepTitle = function( step ) {

// Max length for title.
// Line longer than this will be cutted.
const MAX_TITLE_LEN = 40;

if ( step.title ) {
return step.title;
}

// Neither title nor id is defined
if ( step.id.startsWith( 'step-' ) ) {
for ( var line of step.innerText.split( '\n' ) ) {
line = line.trim( );
if ( line.length > 0 ) {
if ( line.length <= MAX_TITLE_LEN ) {
return line;
} else {
return line.slice( 0, MAX_TITLE_LEN - 3 ) + '...';
}
}
}
}

return step.id;
};

var selectOptionsHtml = function() {
var options = '';
for ( var i = 0; i < steps.length; i++ ) {
Expand All @@ -3960,7 +3987,7 @@
if ( hideSteps.indexOf( steps[ i ] ) < 0 ) {
options = options + '<option value="' + steps[ i ].id + '">' + // jshint ignore:line
(
steps[ i ].title ? steps[ i ].title : steps[ i ].id
getStepTitle( steps[ i ] )
) + '</option>' + '\n';
}
}
Expand Down
29 changes: 28 additions & 1 deletion src/plugins/navigation-ui/navigation-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@
return tempDiv.firstChild;
};

var getStepTitle = function( step ) {

// Max length for title.
// Line longer than this will be cutted.
const MAX_TITLE_LEN = 40;

if ( step.title ) {
return step.title;
}

// Neither title nor id is defined
if ( step.id.startsWith( 'step-' ) ) {
for ( var line of step.innerText.split( '\n' ) ) {
line = line.trim( );
if ( line.length > 0 ) {
if ( line.length <= MAX_TITLE_LEN ) {
return line;
} else {
return line.slice( 0, MAX_TITLE_LEN - 3 ) + '...';
}
}
}
}

return step.id;
};

var selectOptionsHtml = function() {
var options = '';
for ( var i = 0; i < steps.length; i++ ) {
Expand All @@ -46,7 +73,7 @@
if ( hideSteps.indexOf( steps[ i ] ) < 0 ) {
options = options + '<option value="' + steps[ i ].id + '">' + // jshint ignore:line
(
steps[ i ].title ? steps[ i ].title : steps[ i ].id
getStepTitle( steps[ i ] )
) + '</option>' + '\n';
}
}
Expand Down

0 comments on commit b572362

Please sign in to comment.