Add info message on empty toc (#567)

This commit is contained in:
Erik Michelson 2020-09-15 10:22:49 +02:00 committed by GitHub
parent 5972932d33
commit 8842fff06f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View file

@ -153,6 +153,7 @@
"untitledNote": "Untitled", "untitledNote": "Untitled",
"placeholder": "← Start by entering a title here\n===\nVisit the features page if you don't know what to do.\nHappy hacking :)", "placeholder": "← Start by entering a title here\n===\nVisit the features page if you don't know what to do.\nHappy hacking :)",
"invalidYaml": "The yaml-header is invalid. See <0></0> for more information.", "invalidYaml": "The yaml-header is invalid. See <0></0> for more information.",
"infoToc": "Structure your note with headings to see a table-of-contents here.",
"help": { "help": {
"shortcuts": { "shortcuts": {
"title": "Shortcuts", "title": "Shortcuts",

View file

@ -1,4 +1,5 @@
import React, { Fragment, ReactElement, useMemo } from 'react' import React, { Fragment, ReactElement, useMemo } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { TocAst } from '../../../external-types/markdown-it-toc-done-right/interface' import { TocAst } from '../../../external-types/markdown-it-toc-done-right/interface'
import { ShowIf } from '../../common/show-if/show-if' import { ShowIf } from '../../common/show-if/show-if'
import './table-of-contents.scss' import './table-of-contents.scss'
@ -52,10 +53,14 @@ const convertLevel = (toc: TocAst, levelsToShowUnderThis: number, headerCounts:
} }
export const TableOfContents: React.FC<TableOfContentsProps> = ({ ast, maxDepth = 3, className }) => { export const TableOfContents: React.FC<TableOfContentsProps> = ({ ast, maxDepth = 3, className }) => {
useTranslation()
const tocTree = useMemo(() => convertLevel(ast, maxDepth, new Map<string, number>(), false), [ast, maxDepth]) const tocTree = useMemo(() => convertLevel(ast, maxDepth, new Map<string, number>(), false), [ast, maxDepth])
return ( return (
<div className={`markdown-toc ${className ?? ''}`}> <div className={`markdown-toc ${className ?? ''}`}>
<ShowIf condition={ast.c.length === 0}>
<Trans i18nKey={'editor.infoToc'}/>
</ShowIf>
{ tocTree } { tocTree }
</div> </div>
) )