Skip to content

Commit

Permalink
Improve content editor title
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Nov 9, 2023
1 parent f757a96 commit 01ee4d5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/lib/components/contents/details/toolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
revertChanges,
saveEntry,
} from '$lib/services/contents/editor';
import { formatSummary } from '$lib/services/contents/view';
import { goBack, goto } from '$lib/services/navigation';
import { truncate } from '$lib/services/utils/strings';
let showDuplicateToast = false;
let showValidationToast = false;
Expand All @@ -40,6 +42,7 @@
currentValues,
} = $entryDraft ?? /** @type {EntryDraft} */ ({}));
$: ({ defaultLocale = 'default' } = collection?._i18n ?? /** @type {I18nConfig} */ ({}));
$: collectionLabel = collection?.label || collection?.name;
$: collectionLabelSingular = collection?.label_singular || collectionLabel;
$: canPreview =
Expand Down Expand Up @@ -79,7 +82,10 @@
values: {
name: collectionFile
? `${collectionLabel} » ${collectionFile.label}`
: collectionLabelSingular,
: `${collectionLabel} » ${truncate(
formatSummary(collection, originalEntry, defaultLocale, { useTemplate: false }),
25,
)}`,
},
})}
{/if}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/contents/list/entry-list-item.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
{/if}
<TableCell class="title">
<span>
{formatSummary($selectedCollection, entry, content, locale)}
{formatSummary($selectedCollection, entry, locale)}
</span>
</TableCell>
</TableRow>
17 changes: 9 additions & 8 deletions src/lib/services/contents/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { prefs } from '$lib/services/prefs';
import { getDateTimeParts } from '$lib/services/utils/datetime';
import LocalStorage from '$lib/services/utils/local-storage';
import { stripSlashes } from '$lib/services/utils/strings';
import { stripSlashes, truncate } from '$lib/services/utils/strings';

const storageKey = 'sveltia-cms.contents-view';
/**
Expand Down Expand Up @@ -79,11 +79,9 @@ const transformSummary = (summary, tf, fieldConfig) => {
}

if (tf.startsWith('truncate')) {
const [, number, string = ''] = tf.match(/^truncate\((\d+)(?:,\s*'?(.*?)'?)?\)$/);
const max = Number(number);
const truncated = String(summary).substring(0, max);
const [, max, ellipsis = ''] = tf.match(/^truncate\((\d+)(?:,\s*'?(.*?)'?)?\)$/);

return String(summary).length > max ? `${truncated}${string}` : truncated;
return truncate(String(summary), Number(max), { ellipsis });
}

return summary;
Expand All @@ -93,12 +91,15 @@ const transformSummary = (summary, tf, fieldConfig) => {
* Parse the collection summary template to generate the summary to be displayed on the entry list.
* @param {Collection} collection Entry’s collection.
* @param {Entry} entry Entry.
* @param {EntryContent} content Entry content.
* @param {LocaleCode} locale Locale.
* @param {object} [options] Options.
* @param {boolean} [options.useTemplate] Whether to use the collection’s template if available.
* @returns {string} Formatted summary.
* @see https://decapcms.org/docs/configuration-options/#summary
*/
export const formatSummary = (collection, entry, content, locale) => {
export const formatSummary = (collection, entry, locale, { useTemplate = true } = {}) => {
const { content } = entry.locales[locale];

const {
name: collectionName,
folder: collectionFolder,
Expand All @@ -110,7 +111,7 @@ export const formatSummary = (collection, entry, content, locale) => {
// Fields other than `title` should be defined with `identifier_field` as per the Netlify/Decap
// CMS document, but actually `name` also works as a fallback. We also use the label` property and
// the entry slug.
if (!summaryTemplate) {
if (!useTemplate || !summaryTemplate) {
return content[identifierField] || content.title || content.name || content.label || entry.slug;
}

Expand Down
17 changes: 17 additions & 0 deletions src/lib/services/utils/strings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Truncate the given string.
* @param {string} string Original string.
* @param {number} max Maximum number of characters.
* @param {object} [options] Options.
* @param {string} [options.ellipsis] Character(s) to be appended if the the truncated string is
* longer than `max`.
* @returns {string} Truncated string.
*/
export const truncate = (string, max, { ellipsis = '…' } = {}) => {
// Don’t use `split()` because it breaks Unicode characters like emoji
const chars = [...string];
const truncated = chars.slice(0, max).join('').trim();

return `${truncated}${chars.length > max ? ellipsis : ''}`;
};

/**
* Escape the given string so it can be used safely for `new RegExp()`.
* @param {string} string Original string.
Expand Down

0 comments on commit 01ee4d5

Please sign in to comment.