mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
Merge pull request #2585 from daiyam/fix-mermaid-height
fix height of mermaid diagrams
This commit is contained in:
@@ -246,6 +246,7 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
this.saveAsHtmlHandler = () => this.handleSaveAsHtml()
|
this.saveAsHtmlHandler = () => this.handleSaveAsHtml()
|
||||||
this.saveAsPdfHandler = () => this.handleSaveAsPdf()
|
this.saveAsPdfHandler = () => this.handleSaveAsPdf()
|
||||||
this.printHandler = () => this.handlePrint()
|
this.printHandler = () => this.handlePrint()
|
||||||
|
this.resizeHandler = _.throttle(this.handleResize.bind(this), 100)
|
||||||
|
|
||||||
this.linkClickHandler = this.handleLinkClick.bind(this)
|
this.linkClickHandler = this.handleLinkClick.bind(this)
|
||||||
this.initMarkdown = this.initMarkdown.bind(this)
|
this.initMarkdown = this.initMarkdown.bind(this)
|
||||||
@@ -540,6 +541,10 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
'scroll',
|
'scroll',
|
||||||
this.scrollHandler
|
this.scrollHandler
|
||||||
)
|
)
|
||||||
|
this.refs.root.contentWindow.addEventListener(
|
||||||
|
'resize',
|
||||||
|
this.resizeHandler
|
||||||
|
)
|
||||||
eventEmitter.on('export:save-text', this.saveAsTextHandler)
|
eventEmitter.on('export:save-text', this.saveAsTextHandler)
|
||||||
eventEmitter.on('export:save-md', this.saveAsMdHandler)
|
eventEmitter.on('export:save-md', this.saveAsMdHandler)
|
||||||
eventEmitter.on('export:save-html', this.saveAsHtmlHandler)
|
eventEmitter.on('export:save-html', this.saveAsHtmlHandler)
|
||||||
@@ -578,6 +583,10 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
'scroll',
|
'scroll',
|
||||||
this.scrollHandler
|
this.scrollHandler
|
||||||
)
|
)
|
||||||
|
this.refs.root.contentWindow.removeEventListener(
|
||||||
|
'resize',
|
||||||
|
this.resizeHandler
|
||||||
|
)
|
||||||
eventEmitter.off('export:save-text', this.saveAsTextHandler)
|
eventEmitter.off('export:save-text', this.saveAsTextHandler)
|
||||||
eventEmitter.off('export:save-md', this.saveAsMdHandler)
|
eventEmitter.off('export:save-md', this.saveAsMdHandler)
|
||||||
eventEmitter.off('export:save-html', this.saveAsHtmlHandler)
|
eventEmitter.off('export:save-html', this.saveAsHtmlHandler)
|
||||||
@@ -997,6 +1006,15 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleResize () {
|
||||||
|
_.forEach(
|
||||||
|
this.refs.root.contentWindow.document.querySelectorAll('svg[ratio]'),
|
||||||
|
el => {
|
||||||
|
el.setAttribute('height', el.clientWidth / el.getAttribute('ratio'))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
focus () {
|
focus () {
|
||||||
this.refs.root.focus()
|
this.refs.root.focus()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -427,6 +427,9 @@ pre.fence
|
|||||||
canvas, svg
|
canvas, svg
|
||||||
max-width 100% !important
|
max-width 100% !important
|
||||||
|
|
||||||
|
svg[ratio]
|
||||||
|
width 100%
|
||||||
|
|
||||||
.gallery
|
.gallery
|
||||||
width 100%
|
width 100%
|
||||||
height 50vh
|
height 50vh
|
||||||
|
|||||||
@@ -22,18 +22,40 @@ function getId () {
|
|||||||
function render (element, content, theme, enableHTMLLabel) {
|
function render (element, content, theme, enableHTMLLabel) {
|
||||||
try {
|
try {
|
||||||
const height = element.attributes.getNamedItem('data-height')
|
const height = element.attributes.getNamedItem('data-height')
|
||||||
if (height && height.value !== 'undefined') {
|
const isPredefined = height && height.value !== 'undefined'
|
||||||
|
if (isPredefined) {
|
||||||
element.style.height = height.value + 'vh'
|
element.style.height = height.value + 'vh'
|
||||||
}
|
}
|
||||||
const isDarkTheme = theme === 'dark' || theme === 'solarized-dark' || theme === 'monokai' || theme === 'dracula'
|
const isDarkTheme = theme === 'dark' || theme === 'solarized-dark' || theme === 'monokai' || theme === 'dracula'
|
||||||
mermaidAPI.initialize({
|
mermaidAPI.initialize({
|
||||||
theme: isDarkTheme ? 'dark' : 'default',
|
theme: isDarkTheme ? 'dark' : 'default',
|
||||||
themeCSS: isDarkTheme ? darkThemeStyling : '',
|
themeCSS: isDarkTheme ? darkThemeStyling : '',
|
||||||
useMaxWidth: false,
|
flowchart: {
|
||||||
flowchart: { htmlLabels: enableHTMLLabel }
|
htmlLabels: enableHTMLLabel
|
||||||
|
},
|
||||||
|
gantt: {
|
||||||
|
useWidth: element.clientWidth
|
||||||
|
}
|
||||||
})
|
})
|
||||||
mermaidAPI.render(getId(), content, (svgGraph) => {
|
mermaidAPI.render(getId(), content, (svgGraph) => {
|
||||||
element.innerHTML = svgGraph
|
element.innerHTML = svgGraph
|
||||||
|
|
||||||
|
if (!isPredefined) {
|
||||||
|
const el = element.firstChild
|
||||||
|
const viewBox = el.getAttribute('viewBox').split(' ')
|
||||||
|
|
||||||
|
let ratio = viewBox[2] / viewBox[3]
|
||||||
|
|
||||||
|
if (el.style.maxWidth) {
|
||||||
|
const maxWidth = parseFloat(el.style.maxWidth)
|
||||||
|
|
||||||
|
ratio *= el.parentNode.clientWidth / maxWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
el.setAttribute('ratio', ratio)
|
||||||
|
el.setAttribute('height', el.parentNode.clientWidth / ratio)
|
||||||
|
console.log(el)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
element.className = 'mermaid-error'
|
element.className = 'mermaid-error'
|
||||||
|
|||||||
Reference in New Issue
Block a user