Feature/lazy load components (#590)

This commit is contained in:
mrdrogdrog 2020-09-26 09:54:17 +02:00 committed by GitHub
parent 9c38655a92
commit 101292da92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 261 additions and 248 deletions

View file

@ -1,35 +1,38 @@
import { Editor, Hint, Hints, Pos } from 'codemirror'
import hljs from 'highlight.js'
import { findWordAtCursor, Hinter, search } from './index'
const allowedChars = /[`\w-_+]/
const wordRegExp = /^```((\w|-|_|\+)*)$/
const allSupportedLanguages = hljs.listLanguages().concat('csv', 'flow', 'html')
let allSupportedLanguages: string[] = []
const codeBlockHint = (editor: Editor): Promise< Hints| null > => {
return new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
return
}
const term = searchResult[1]
const suggestions = search(term, allSupportedLanguages)
const cursor = editor.getCursor()
if (!suggestions) {
resolve(null)
} else {
resolve({
list: suggestions.map((suggestion: string): Hint => ({
text: '```' + suggestion + '\n\n```\n',
displayText: suggestion
})),
from: Pos(cursor.line, searchTerm.start),
to: Pos(cursor.line, searchTerm.end)
})
}
})
return import(/* webpackChunkName: "highlight.js" */ 'highlight.js').then(hljs =>
new Promise((resolve) => {
const searchTerm = findWordAtCursor(editor, allowedChars)
const searchResult = wordRegExp.exec(searchTerm.text)
if (searchResult === null) {
resolve(null)
return
}
const term = searchResult[1]
if (allSupportedLanguages.length === 0) {
allSupportedLanguages = hljs.listLanguages().concat('csv', 'flow', 'html')
}
const suggestions = search(term, allSupportedLanguages)
const cursor = editor.getCursor()
if (!suggestions) {
resolve(null)
} else {
resolve({
list: suggestions.map((suggestion: string): Hint => ({
text: '```' + suggestion + '\n\n```\n',
displayText: suggestion
})),
from: Pos(cursor.line, searchTerm.start),
to: Pos(cursor.line, searchTerm.end)
})
}
}))
}
export const CodeBlockHinter: Hinter = {