From 6414048c95fea7191e1c605f411efc426fe6908f Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Thu, 28 Mar 2024 19:10:10 +0800 Subject: [PATCH] improve loading of tags + metadata --- MyApp/wwwroot/lib/js/default.js | 32 ++++++++++++++++++ MyApp/wwwroot/mjs/app.mjs | 2 +- MyApp/wwwroot/pages/Questions/Answer.mjs | 42 +++++++----------------- 3 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 MyApp/wwwroot/lib/js/default.js diff --git a/MyApp/wwwroot/lib/js/default.js b/MyApp/wwwroot/lib/js/default.js new file mode 100644 index 0000000..9f31aab --- /dev/null +++ b/MyApp/wwwroot/lib/js/default.js @@ -0,0 +1,32 @@ +window.hljs?.highlightAll() + +if (!localStorage.getItem('data:tags.txt')) +{ + fetch('/data/tags.txt') + .then(r => r.text()) + .then(txt => localStorage.setItem('data:tags.txt', txt)); +} + +function metadataDate(metadataJson) { + try { + if (metadataJson) { + return new Date(parseInt(metadataJson.match(/Date\((\d+)\)/)[1])) + } + } catch{} + return new Date() - (24 * 60 * 60 * 1000) +} + +const metadataJson = localStorage.getItem('/metadata/app.json') +const oneHourAgo = new Date() - 60 * 60 * 1000 +const clearMetadata = !metadataJson + || location.search.includes('clear=metadata') + || metadataDate(metadataJson) < oneHourAgo + +if (clearMetadata) { + fetch('/metadata/app.json') + .then(r => r.text()) + .then(json => { + console.log('updating /metadata/app.json...') + localStorage.setItem('/metadata/app.json', json) + }) +} diff --git a/MyApp/wwwroot/mjs/app.mjs b/MyApp/wwwroot/mjs/app.mjs index efda390..183d324 100644 --- a/MyApp/wwwroot/mjs/app.mjs +++ b/MyApp/wwwroot/mjs/app.mjs @@ -15,7 +15,7 @@ const CustomElements = [ 'lite-youtube' ] -const alreadyMounted = el => el.__vue_app__ +export const alreadyMounted = el => el.__vue_app__ const mockArgs = { attrs:{}, slots:{}, emit:() => {}, expose: () => {} } function hasTemplate(el,component) { diff --git a/MyApp/wwwroot/pages/Questions/Answer.mjs b/MyApp/wwwroot/pages/Questions/Answer.mjs index 11daf4e..ec3ab6e 100644 --- a/MyApp/wwwroot/pages/Questions/Answer.mjs +++ b/MyApp/wwwroot/pages/Questions/Answer.mjs @@ -6,8 +6,7 @@ import { AnswerQuestion, PreviewMarkdown } from "dtos.mjs" export default { template:`
- + -
-

Preview

-
+
+

+ + Preview +

+
@@ -36,26 +38,23 @@ export default {
`, - props:['id'], + props:['id','title','body','refId'], setup(props) { const { user } = useAuth() const client = useClient() const autoform = ref() - const request = ref(new AnswerQuestion()) + const request = ref(new AnswerQuestion(props)) const qs = queryString(location.search) request.value.postId = props.id if (qs.title) request.value.title = qs.title if (qs.body) request.value.body = qs.body - if (qs.tags) request.value.tags = qs.tags.split(',') if (qs.refId || qs.refid) request.value.refId = qs.refId || qs.refid const previewHtml = ref('') - let allTags = localStorage.getItem('data:tags.txt')?.split('\n') || [] + const expandPreview = ref(true) const { createDebounce } = useUtils() let lastBody = '' - let i = 0 - let tagsInput = null const debounceApi = createDebounce(async markdown => { if (lastBody === request.value.body) return @@ -75,27 +74,8 @@ export default { location.reload() } - onMounted(async () => { - if (allTags.length === 0) { - const txt = await (await fetch('/data/tags.txt')).text() - localStorage.setItem('data:tags.txt', txt) - allTags = txt.split('\n') - // TODO doesn't work - if (tagsInput != null) { - tagsInput.allowableValues = allTags - } - } - }) - - function configureField(inputProp) { - if (inputProp.type === 'tag') { - tagsInput = inputProp - inputProp.allowableValues = allTags - } - } - const signInUrl = () => `/Account/Login?ReturnUrl=${location.pathname}` - return { user, request, previewHtml, autoform, signInUrl, configureField, onSuccess } + return { user, request, previewHtml, expandPreview, autoform, signInUrl, onSuccess } } }