diff --git a/develop/docker-compose.yml b/develop/docker-compose.yml index 750e11ac87..990d840b63 100644 --- a/develop/docker-compose.yml +++ b/develop/docker-compose.yml @@ -123,14 +123,14 @@ services: dockerfile: services/real-time/Dockerfile env_file: - dev.env - + redis: image: redis:5 ports: - "127.0.0.1:6379:6379" # for debugging volumes: - redis-data:/data - + web: build: context: .. diff --git a/services/filestore/app.js b/services/filestore/app.js index 24741e079c..178e8c7ff0 100644 --- a/services/filestore/app.js +++ b/services/filestore/app.js @@ -111,6 +111,11 @@ if (settings.filestore.stores.template_files) { keyBuilder.templateFileKeyMiddleware, fileController.insertFile ) + app.delete( + '/template/:template_id/v/:version/:format', + keyBuilder.templateFileKeyMiddleware, + fileController.deleteFile + ) } app.get( diff --git a/services/filestore/app/js/FileConverter.js b/services/filestore/app/js/FileConverter.js index ac3dccec1f..bfc34314e9 100644 --- a/services/filestore/app/js/FileConverter.js +++ b/services/filestore/app/js/FileConverter.js @@ -5,7 +5,7 @@ const { callbackify } = require('node:util') const safeExec = require('./SafeExec').promises const { ConversionError } = require('./Errors') -const APPROVED_FORMATS = ['png'] +const APPROVED_FORMATS = ['png', 'jpg'] const FOURTY_SECONDS = 40 * 1000 const KILL_SIGNAL = 'SIGTERM' @@ -34,16 +34,14 @@ async function convert(sourcePath, requestedFormat) { } async function thumbnail(sourcePath) { - const width = '260x' - return await convert(sourcePath, 'png', [ + const width = '548x' + return await _convert(sourcePath, 'jpg', [ 'convert', '-flatten', '-background', 'white', '-density', '300', - '-define', - `pdf:fit-page=${width}`, `${sourcePath}[0]`, '-resize', width, @@ -51,16 +49,14 @@ async function thumbnail(sourcePath) { } async function preview(sourcePath) { - const width = '548x' - return await convert(sourcePath, 'png', [ + const width = '794x' + return await _convert(sourcePath, 'jpg', [ 'convert', '-flatten', '-background', 'white', '-density', '300', - '-define', - `pdf:fit-page=${width}`, `${sourcePath}[0]`, '-resize', width, diff --git a/services/filestore/app/js/FileHandler.js b/services/filestore/app/js/FileHandler.js index 2ed28bd435..0c092c85cd 100644 --- a/services/filestore/app/js/FileHandler.js +++ b/services/filestore/app/js/FileHandler.js @@ -150,7 +150,9 @@ async function _getConvertedFileAndCache(bucket, key, convertedKey, opts) { let convertedFsPath try { convertedFsPath = await _convertFile(bucket, key, opts) - await ImageOptimiser.promises.compressPng(convertedFsPath) + if (convertedFsPath.toLowerCase().endsWith(".png")) { + await ImageOptimiser.promises.compressPng(convertedFsPath) + } await PersistorManager.sendFile(bucket, convertedKey, convertedFsPath) } catch (err) { LocalFileWriter.deleteFile(convertedFsPath, () => {}) diff --git a/services/web/app/src/Features/Project/ProjectController.js b/services/web/app/src/Features/Project/ProjectController.js index ec128ffd54..79aaef9334 100644 --- a/services/web/app/src/Features/Project/ProjectController.js +++ b/services/web/app/src/Features/Project/ProjectController.js @@ -599,7 +599,7 @@ const _ProjectController = { } const isAdminOrTemplateOwner = - hasAdminAccess(user) || Settings.templates?.user_id === userId + hasAdminAccess(user) || Settings.templates?.nonAdminCanManage const showTemplatesServerPro = Features.hasFeature('templates-server-pro') && isAdminOrTemplateOwner diff --git a/services/web/app/src/Features/Templates/TemplatesController.js b/services/web/app/src/Features/Templates/TemplatesController.js index a8730a61be..b238527430 100644 --- a/services/web/app/src/Features/Templates/TemplatesController.js +++ b/services/web/app/src/Features/Templates/TemplatesController.js @@ -11,21 +11,22 @@ const TemplatesController = { // Read split test assignment so that it's available for Pug to read await SplitTestHandler.promises.getAssignment(req, res, 'core-pug-bs5') - const templateVersionId = req.params.Template_version_id - const templateId = req.query.id - if (!/^[0-9]+$/.test(templateVersionId) || !/^[0-9]+$/.test(templateId)) { - logger.err( - { templateVersionId, templateId }, - 'invalid template id or version' - ) - return res.sendStatus(400) - } + const templateId = req.params.Template_version_id + const templateVersionId = req.query.version +// if (!/^[0-9]+$/.test(templateVersionId) || !/^[0-9]+$/.test(templateId)) { +// logger.err( +// { templateVersionId, templateId }, +// 'invalid template id or version' +// ) +// return res.sendStatus(400) +// } const data = { templateVersionId, templateId, - name: req.query.templateName, - compiler: ProjectHelper.compilerFromV1Engine(req.query.latexEngine), - imageName: req.query.texImage, + name: req.query.name, + compiler: req.query.compiler, + language: req.query.language, + imageName: req.query.imageName, mainFile: req.query.mainFile, brandVariationId: req.query.brandVariationId, } @@ -40,6 +41,7 @@ const TemplatesController = { async createProjectFromV1Template(req, res) { const userId = SessionManager.getLoggedInUserId(req.session) + const project = await TemplatesManager.promises.createProjectFromV1Template( req.body.brandVariationId, req.body.compiler, @@ -48,7 +50,8 @@ const TemplatesController = { req.body.templateName, req.body.templateVersionId, userId, - req.body.imageName + req.body.imageName, + req.body.language ) delete req.session.templateData if (!project) { diff --git a/services/web/app/src/Features/Templates/TemplatesManager.js b/services/web/app/src/Features/Templates/TemplatesManager.js index 6a2b6207c1..77cde12ea9 100644 --- a/services/web/app/src/Features/Templates/TemplatesManager.js +++ b/services/web/app/src/Features/Templates/TemplatesManager.js @@ -18,6 +18,7 @@ const crypto = require('crypto') const Errors = require('../Errors/Errors') const { pipeline } = require('stream/promises') const ClsiCacheManager = require('../Compile/ClsiCacheManager') +const TIMEOUT = 30000 // 30 sec const TemplatesManager = { async createProjectFromV1Template( @@ -28,25 +29,19 @@ const TemplatesManager = { templateName, templateVersionId, userId, - imageName + imageName, + language ) { - const zipUrl = `${settings.apis.v1.url}/api/v1/overleaf/templates/${templateVersionId}` + const zipUrl = `${settings.apis.filestore.url}/template/${templateId}/v/${templateVersionId}/zip` const zipReq = await fetchStreamWithResponse(zipUrl, { - basicAuth: { - user: settings.apis.v1.user, - password: settings.apis.v1.pass, - }, - signal: AbortSignal.timeout(settings.apis.v1.timeout), + signal: AbortSignal.timeout(TIMEOUT), }) const projectName = ProjectDetailsHandler.fixProjectName(templateName) const dumpPath = `${settings.path.dumpFolder}/${crypto.randomUUID()}` const writeStream = fs.createWriteStream(dumpPath) try { - const attributes = { - fromV1TemplateId: templateId, - fromV1TemplateVersionId: templateVersionId, - } + const attributes = {} await pipeline(zipReq.stream, writeStream) if (zipReq.response.status !== 200) { @@ -78,14 +73,9 @@ const TemplatesManager = { await TemplatesManager._setCompiler(project._id, compiler) await TemplatesManager._setImage(project._id, imageName) await TemplatesManager._setMainFile(project._id, mainFile) + await TemplatesManager._setSpellCheckLanguage(project._id, language) await TemplatesManager._setBrandVariationId(project._id, brandVariationId) - const update = { - fromV1TemplateId: templateId, - fromV1TemplateVersionId: templateVersionId, - } - await Project.updateOne({ _id: project._id }, update, {}) - await prepareClsiCacheInBackground return project @@ -102,11 +92,12 @@ const TemplatesManager = { }, async _setImage(projectId, imageName) { - if (!imageName) { - imageName = 'wl_texlive:2018.1' + try { + await ProjectOptionsHandler.setImageName(projectId, imageName) + } catch { + logger.warn({ imageName: imageName }, 'not available') + await ProjectOptionsHandler.setImageName(projectId, settings.currentImageName) } - - await ProjectOptionsHandler.setImageName(projectId, imageName) }, async _setMainFile(projectId, mainFile) { @@ -116,6 +107,13 @@ const TemplatesManager = { await ProjectRootDocManager.setRootDocFromName(projectId, mainFile) }, + async _setSpellCheckLanguage(projectId, language) { + if (language == null) { + return + } + await ProjectOptionsHandler.setSpellCheckLanguage(projectId, language) + }, + async _setBrandVariationId(projectId, brandVariationId) { if (brandVariationId == null) { return diff --git a/services/web/app/src/infrastructure/ExpressLocals.js b/services/web/app/src/infrastructure/ExpressLocals.js index eae1b48219..d507ef24e0 100644 --- a/services/web/app/src/infrastructure/ExpressLocals.js +++ b/services/web/app/src/infrastructure/ExpressLocals.js @@ -428,7 +428,7 @@ module.exports = function (webRouter, privateApiRouter, publicApiRouter) { labsEnabled: Settings.labs && Settings.labs.enable, wikiEnabled: Settings.overleaf != null || Settings.proxyLearn, templatesEnabled: - Settings.overleaf != null || Settings.templates?.user_id != null, + Settings.overleaf != null || Boolean(Settings.templates), cioWriteKey: Settings.analytics?.cio?.writeKey, cioSiteId: Settings.analytics?.cio?.siteId, } diff --git a/services/web/app/src/infrastructure/Features.js b/services/web/app/src/infrastructure/Features.js index aaf51103b9..ed231a6caa 100644 --- a/services/web/app/src/infrastructure/Features.js +++ b/services/web/app/src/infrastructure/Features.js @@ -69,7 +69,7 @@ const Features = { case 'oauth': return Boolean(Settings.oauth) case 'templates-server-pro': - return Boolean(Settings.templates?.user_id) + return Boolean(Settings.templates) case 'affiliations': case 'analytics': return Boolean(_.get(Settings, ['apis', 'v1', 'url'])) diff --git a/services/web/app/src/router.mjs b/services/web/app/src/router.mjs index a7e8d5e05f..1f22a57f9f 100644 --- a/services/web/app/src/router.mjs +++ b/services/web/app/src/router.mjs @@ -262,6 +262,8 @@ async function initialize(webRouter, privateApiRouter, publicApiRouter) { '/read-only/one-time-login' ) + await Modules.applyRouter(webRouter, privateApiRouter, publicApiRouter) + webRouter.post('/logout', UserController.logout) webRouter.get('/restricted', AuthorizationMiddleware.restricted) @@ -285,8 +287,6 @@ async function initialize(webRouter, privateApiRouter, publicApiRouter) { TokenAccessRouter.apply(webRouter) HistoryRouter.apply(webRouter, privateApiRouter) - await Modules.applyRouter(webRouter, privateApiRouter, publicApiRouter) - if (Settings.enableSubscriptions) { webRouter.get( '/user/bonus', diff --git a/services/web/app/views/layout/navbar-marketing-bootstrap-5.pug b/services/web/app/views/layout/navbar-marketing-bootstrap-5.pug index 92e2d4301d..69e7edd9b3 100644 --- a/services/web/app/views/layout/navbar-marketing-bootstrap-5.pug +++ b/services/web/app/views/layout/navbar-marketing-bootstrap-5.pug @@ -146,6 +146,18 @@ nav.navbar.navbar-default.navbar-main.navbar-expand-lg(class={ event-segmentation={ page: currentUrl, item: 'register', location: 'top-menu' } ) #{translate('sign_up')} + // templates link + if settings.templates + +nav-item + +nav-link( + href="/templates" + event-tracking="menu-click" + event-tracking-action="clicked" + event-tracking-trigger="click" + event-tracking-mb="true" + event-segmentation={ page: currentUrl, item: 'templates', location: 'top-menu' } + ) #{translate('templates')} + // login link +nav-item +nav-link( diff --git a/services/web/app/views/layout/navbar-marketing.pug b/services/web/app/views/layout/navbar-marketing.pug index c5e9f2e0bf..c3b23c4853 100644 --- a/services/web/app/views/layout/navbar-marketing.pug +++ b/services/web/app/views/layout/navbar-marketing.pug @@ -140,6 +140,18 @@ nav.navbar.navbar-default.navbar-main(class={ // logged out if !getSessionUser() + // templates link + if settings.templates + li + a( + href="/templates" + event-tracking="menu-click" + event-tracking-action="clicked" + event-tracking-trigger="click" + event-tracking-mb="true" + event-segmentation={ page: currentUrl, item: 'templates', location: 'top-menu' } + ) #{translate('templates')} + // register link if hasFeature('registration-page') li.primary diff --git a/services/web/app/views/project/editor/new_from_template.pug b/services/web/app/views/project/editor/new_from_template.pug index f2945b20f1..fca70cac00 100644 --- a/services/web/app/views/project/editor/new_from_template.pug +++ b/services/web/app/views/project/editor/new_from_template.pug @@ -31,8 +31,10 @@ block content input(type="hidden" name="templateVersionId" value=templateVersionId) input(type="hidden" name="templateName" value=name) input(type="hidden" name="compiler" value=compiler) - input(type="hidden" name="imageName" value=imageName) + if imageName + input(type="hidden" name="imageName" value=imageName) input(type="hidden" name="mainFile" value=mainFile) + input(type="hidden" name="language" value=language) if brandVariationId input(type="hidden" name="brandVariationId" value=brandVariationId) input(hidden type="submit") diff --git a/services/web/app/views/template_gallery/template-gallery.pug b/services/web/app/views/template_gallery/template-gallery.pug new file mode 100644 index 0000000000..3838d30606 --- /dev/null +++ b/services/web/app/views/template_gallery/template-gallery.pug @@ -0,0 +1,18 @@ +extends ../layout-react + +block entrypointVar + - entrypoint = 'pages/template-gallery' + +block vars +block vars + - const suppressNavContentLinks = true + - const suppressNavbar = true + - const suppressFooter = true + - bootstrap5PageStatus = 'enabled' // One of 'disabled', 'enabled', and 'queryStringOnly' + - isWebsiteRedesign = false + +block append meta + meta(name="ol-templateCategory" data-type="string" content=category) + +block content + #template-gallery-root diff --git a/services/web/app/views/template_gallery/template.pug b/services/web/app/views/template_gallery/template.pug new file mode 100644 index 0000000000..e56fd8d2e5 --- /dev/null +++ b/services/web/app/views/template_gallery/template.pug @@ -0,0 +1,20 @@ +extends ../layout-react + +block entrypointVar + - entrypoint = 'pages/template' + +block vars + - const suppressNavContentLinks = true + - const suppressNavbar = true + - const suppressFooter = true + - bootstrap5PageStatus = 'enabled' // One of 'disabled', 'enabled', and 'queryStringOnly' + - isWebsiteRedesign = false + +block append meta + meta(name="ol-template" data-type="json" content=template) + meta(name="ol-languages" data-type="json" content=languages) + meta(name="ol-userIsAdmin" data-type="boolean" content=hasAdminAccess()) + +block content + #template-root + diff --git a/services/web/config/settings.defaults.js b/services/web/config/settings.defaults.js index a7ff970ef0..ee3423dcda 100644 --- a/services/web/config/settings.defaults.js +++ b/services/web/config/settings.defaults.js @@ -981,7 +981,7 @@ module.exports = { importProjectFromGithubModalWrapper: [], importProjectFromGithubMenu: [], editorLeftMenuSync: [], - editorLeftMenuManageTemplate: [], + editorLeftMenuManageTemplate: ['@/features/editor-left-menu/components/actions-manage-template'], oauth2Server: [], managedGroupSubscriptionEnrollmentNotification: [], managedGroupEnrollmentInvite: [], @@ -1005,6 +1005,7 @@ module.exports = { 'launchpad', 'server-ce-scripts', 'user-activate', + 'template-gallery', ], viewIncludes: {}, diff --git a/services/web/frontend/extracted-translations.json b/services/web/frontend/extracted-translations.json index 9862e47817..d13f66f701 100644 --- a/services/web/frontend/extracted-translations.json +++ b/services/web/frontend/extracted-translations.json @@ -23,6 +23,7 @@ "about_to_delete_cert": "", "about_to_delete_projects": "", "about_to_delete_tag": "", + "about_to_delete_template": "", "about_to_delete_the_following_project": "", "about_to_delete_the_following_projects": "", "about_to_delete_user_preamble": "", @@ -124,6 +125,7 @@ "all_premium_features_including": "", "all_projects": "", "all_projects_will_be_transferred_immediately": "", + "all_templates": "", "all_these_experiments_are_available_exclusively": "", "allows_to_search_by_author_title_etc_possible_to_pull_results_directly_from_your_reference_manager_if_connected": "", "already_have_a_papers_account": "", @@ -154,6 +156,7 @@ "ask_repo_owner_to_reconnect": "", "ask_repo_owner_to_renew_overleaf_subscription": "", "at_most_x_libraries_can_be_selected": "", + "author": "", "auto_close_brackets": "", "auto_compile": "", "auto_complete": "", @@ -215,6 +218,8 @@ "card_must_be_authenticated_by_3dsecure": "", "card_payment": "", "careers": "", + "categories": "", + "category": "", "category_arrows": "", "category_greek": "", "category_misc": "", @@ -352,6 +357,7 @@ "customize_your_group_subscription": "", "customizing_figures": "", "customizing_tables": "", + "date": "", "date_and_owner": "", "dealing_with_errors": "", "decrease_indent": "", @@ -377,6 +383,7 @@ "delete_sso_config": "", "delete_table": "", "delete_tag": "", + "delete_template": "", "delete_token": "", "delete_user": "", "delete_your_account": "", @@ -476,6 +483,7 @@ "edit_figure": "", "edit_sso_configuration": "", "edit_tag": "", + "edit_template": "", "edit_your_custom_dictionary": "", "editing": "", "editing_captions": "", @@ -890,6 +898,7 @@ "last_name": "", "last_resort_trouble_shooting_guide": "", "last_suggested_fix": "", + "last_updated": "", "last_updated_date_by_x": "", "last_used": "", "latam_discount_modal_info": "", @@ -898,6 +907,8 @@ "latex_in_thirty_minutes": "", "latex_places_figures_according_to_a_special_algorithm": "", "latex_places_tables_according_to_a_special_algorithm": "", + "latex_templates": "", + "latex_templates_for_journal_articles": "", "layout": "", "layout_options": "", "layout_processing": "", @@ -921,7 +932,8 @@ "let_us_know_what_you_think": "", "lets_get_those_premium_features": "", "library": "", - "licenses": "", + "license": "", + "license_for_educational_purposes_confirmation": "", "limited_document_history": "", "limited_offer": "", "limited_to_n_collaborators_per_project": "", @@ -1105,6 +1117,7 @@ "no_selection_select_file": "", "no_symbols_found": "", "no_thanks_cancel_now": "", + "no_templates_found": "", "normal": "", "normally_x_price_per_month": "", "normally_x_price_per_year": "", @@ -1135,6 +1148,7 @@ "only_importer_can_refresh": "", "open_action_menu": "", "open_advanced_reference_search": "", + "open_as_template": "", "open_file": "", "open_link": "", "open_path": "", @@ -1160,6 +1174,7 @@ "overleaf_is_easy_to_use": "", "overleaf_labs": "", "overleaf_logo": "", + "overleaf_template_gallery": "", "overleafs_functionality_meets_my_needs": "", "overview": "", "overwrite": "", @@ -1222,6 +1237,7 @@ "please_change_primary_to_remove": "", "please_check_your_inbox_to_confirm": "", "please_compile_pdf_before_download": "", + "please_compile_pdf_before_publish_as_template": "", "please_compile_pdf_before_word_count": "", "please_confirm_primary_email_or_edit": "", "please_confirm_secondary_email_or_edit": "", @@ -1256,6 +1272,7 @@ "premium_plan_label": "", "presentation_mode": "", "press_and_awards": "", + "prev": "", "previous_page": "", "price": "", "primarily_work_study_question": "", @@ -1709,6 +1726,7 @@ "tell_the_project_owner_and_ask_them_to_upgrade": "", "template": "", "template_description": "", + "template_gallery": "", "template_title_taken_from_project_title": "", "templates": "", "temporarily_hides_the_preview": "", diff --git a/services/web/frontend/js/features/editor-left-menu/components/actions-manage-template.tsx b/services/web/frontend/js/features/editor-left-menu/components/actions-manage-template.tsx new file mode 100644 index 0000000000..f277111a91 --- /dev/null +++ b/services/web/frontend/js/features/editor-left-menu/components/actions-manage-template.tsx @@ -0,0 +1,72 @@ +import { useCallback, useState } from 'react' +import { useTranslation } from 'react-i18next' +import * as eventTracking from '../../../infrastructure/event-tracking' +import getMeta from '../../../utils/meta' +import OLTooltip from '@/features/ui/components/ol/ol-tooltip' +import { useDetachCompileContext } from '../../../shared/context/detach-compile-context' +import EditorManageTemplateModalWrapper from '../../template/components/manage-template-modal/editor-manage-template-modal-wrapper' +import LeftMenuButton from './left-menu-button' + +type TemplateManageResponse = { + template_id: string +} + +export default function ActionsManageTemplate() { + + const templatesAdmin = getMeta('ol-showTemplatesServerPro') + if (!templatesAdmin) { + return null + } + + const [showModal, setShowModal] = useState(false) + const { pdfFile } = useDetachCompileContext() + const { t } = useTranslation() + + const handleShowModal = useCallback(() => { + eventTracking.sendMB('left-menu-template') + setShowModal(true) + }, []) + + const openTemplate = useCallback( + ({ template_id: templateId }: TemplateManageResponse) => { + location.assign(`/template/${templateId}`) + }, + [location] + ) + + return ( + <> + {pdfFile ? ( + + {t('publish_as_template')} + + ) : ( + + {/* OverlayTrigger won't fire unless the child is a non-react html element (e.g div, span) */} + + + {t('publish_as_template')} + + + + )} + setShowModal(false)} + openTemplate={openTemplate} + /> + > + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/gallery-header-all.tsx b/services/web/frontend/js/features/template-gallery/components/gallery-header-all.tsx new file mode 100644 index 0000000000..aed7c311f9 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/gallery-header-all.tsx @@ -0,0 +1,29 @@ +import { useTranslation } from 'react-i18next' +import OLCol from '@/features/ui/components/ol/ol-col' +import OLRow from '@/features/ui/components/ol/ol-row' + +export default function GalleryHeaderAll() { + const { t } = useTranslation() + return ( + + + + + + { + {t('overleaf_template_gallery')} + } + + {t('latex_templates')} + + + + + + {t('latex_templates_for_journal_articles')} + + + + + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/gallery-header-tagged.tsx b/services/web/frontend/js/features/template-gallery/components/gallery-header-tagged.tsx new file mode 100644 index 0000000000..ca2f37dbb5 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/gallery-header-tagged.tsx @@ -0,0 +1,33 @@ +import getMeta from '@/utils/meta' +import OLCol from '@/features/ui/components/ol/ol-col' +import OLRow from '@/features/ui/components/ol/ol-row' +import GallerySearchSortHeader from './gallery-search-sort-header' + +export default function GalleryHeaderTagged({ category }) { + const title = getMeta('og:title') + const { templateLinks } = getMeta('ol-ExposedSettings') || [] + + const description = templateLinks?.find(link => link.url.split("/").pop() === category)?.description + const gotoAllLink = (category !== 'all') + return ( + + + { category && ( + <> + + + {title} + + + + + {description} + + + > + )} + + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/gallery-popular-tags.tsx b/services/web/frontend/js/features/template-gallery/components/gallery-popular-tags.tsx new file mode 100644 index 0000000000..2e5d847d23 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/gallery-popular-tags.tsx @@ -0,0 +1,31 @@ +import { useTranslation } from 'react-i18next' +import getMeta from '@/utils/meta' + +export default function GalleryPopularTags() { + const { t } = useTranslation() + const { templateLinks } = getMeta('ol-ExposedSettings') || [] + + if(!templateLinks || templateLinks.length < 2) return null + + return ( + + {t('categories')} + + {templateLinks?.filter(link => link.url.split("/").pop() !== "all").map((link, index) => ( + + + + + + {link.name} + + {link.description} + + ))} + + + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/gallery-search-sort-header.tsx b/services/web/frontend/js/features/template-gallery/components/gallery-search-sort-header.tsx new file mode 100644 index 0000000000..128d379684 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/gallery-search-sort-header.tsx @@ -0,0 +1,78 @@ +import { useTemplateGalleryContext } from '../context/template-gallery-context' +import { useTranslation } from 'react-i18next' +import SearchForm from './search-form' +import OLCol from '@/features/ui/components/ol/ol-col' +import OLRow from '@/features/ui/components/ol/ol-row' +import useSort from '../hooks/use-sort' +import withContent, { SortBtnProps } from './sort/with-content' +import MaterialIcon from '@/shared/components/material-icon' + +function SortBtn({ onClick, text, iconType, screenReaderText }: SortBtnProps) { + return ( + + {text} + {iconType ? ( + + ) : ( + + )} + + ) +} + +const SortByButton = withContent(SortBtn) + +export default function GallerySearchSortHeader( { gotoAllLink }: { boolean } ) { + const { t } = useTranslation() + const { + searchText, + setSearchText, + sort, + } = useTemplateGalleryContext() + + const { handleSort } = useSort() + return ( + + {gotoAllLink ? ( + + + arrow_left_alt + {t('all_templates')} + + + ) : ( + + + arrow_left_alt + {t('template_gallery')} + + + )} + + handleSort('lastUpdated')} + /> + + handleSort('name')} + /> + + + + + + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/pagination.tsx b/services/web/frontend/js/features/template-gallery/components/pagination.tsx new file mode 100644 index 0000000000..a55f96a6a4 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/pagination.tsx @@ -0,0 +1,80 @@ +import { useTranslation } from 'react-i18next' + +export default function Pagination({ currentPage, totalPages, onPageChange }) { + const { t } = useTranslation() + if (totalPages <= 1) return null + + const pageNumbers = [] + let startPage = Math.max(1, currentPage - 4) + let endPage = Math.min(totalPages, currentPage + 4) + + if (startPage > 1) { + pageNumbers.push(1) + if (startPage > 2) { + pageNumbers.push("...") + } + } + + for (let i = startPage; i <= endPage; i++) { + pageNumbers.push(i) + } + + if (endPage < totalPages) { + if (endPage < totalPages - 1) { + pageNumbers.push("...") + } + pageNumbers.push(totalPages) + } + + return ( + + +{/* + {currentPage > 1 && ( + + onPageChange(1)}> + << {t('first')} + + + )} +*/} + {currentPage > 1 && ( + + onPageChange(currentPage - 1)}> + < {t('prev')} + + + )} + {pageNumbers.map((page, index) => ( + + {page === "..." ? ( + {page} + ) : page === currentPage ? ( + {page} + ) : ( + onPageChange(page)}> + {page} + + )} + + ))} + {currentPage < totalPages && ( + + onPageChange(currentPage + 1)}> + {t('next')} > + + + )} +{/* + {currentPage < totalPages && ( + + onPageChange(totalPages)}> + {t('last')} >> + + + )} +*/} + + + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/search-form.tsx b/services/web/frontend/js/features/template-gallery/components/search-form.tsx new file mode 100644 index 0000000000..75c9a8a313 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/search-form.tsx @@ -0,0 +1,61 @@ +import { useTranslation } from 'react-i18next' +import { MergeAndOverride } from '../../../../../types/utils' +import OLForm from '@/features/ui/components/ol/ol-form' +import OLFormControl from '@/features/ui/components/ol/ol-form-control' +import MaterialIcon from '@/shared/components/material-icon' + +type SearchFormOwnProps = { + inputValue: string + setInputValue: (input: string) => void +} + +type SearchFormProps = MergeAndOverride< + React.ComponentProps, + SearchFormOwnProps +> + +export default function SearchForm({ + inputValue, + setInputValue, +}: SearchFormProps) { + const { t } = useTranslation() + let placeholderMessage = t('search') + const placeholder = `${placeholderMessage}…` + + const handleChange: React.ComponentProps['onChange'] = e => { + setInputValue(e.target.value) + } + + const handleClear = () => setInputValue('') + + return ( + e.preventDefault()} + > + } + append={ + inputValue.length > 0 && ( + + + + ) + } + /> + + ) +} diff --git a/services/web/frontend/js/features/template-gallery/components/sort/with-content.tsx b/services/web/frontend/js/features/template-gallery/components/sort/with-content.tsx new file mode 100644 index 0000000000..8c77484fdf --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/sort/with-content.tsx @@ -0,0 +1,46 @@ +import { useTranslation } from 'react-i18next' +import { Sort } from '../../types/api' + +type SortBtnOwnProps = { + column: string + sort: Sort + text: string + onClick: () => void +} + +type WithContentProps = { + iconType?: string + screenReaderText: string +} + +export type SortBtnProps = SortBtnOwnProps & WithContentProps + +function withContent( + WrappedComponent: React.ComponentType +) { + function WithContent(hocProps: T) { + const { t } = useTranslation() + const { column, text, sort } = hocProps + let iconType + + let screenReaderText = t('sort_by_x', { x: text }) + + if (column === sort.by) { + iconType = + sort.order === 'asc' ? 'arrow_upward_alt' : 'arrow_downward_alt' + screenReaderText = t('reverse_x_sort_order', { x: text }) + } + + return ( + + ) + } + + return WithContent +} + +export default withContent diff --git a/services/web/frontend/js/features/template-gallery/components/template-gallery-entry.tsx b/services/web/frontend/js/features/template-gallery/components/template-gallery-entry.tsx new file mode 100644 index 0000000000..479b306b51 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/template-gallery-entry.tsx @@ -0,0 +1,29 @@ +import { memo } from 'react' +import { cleanHtml } from '../../../../../modules/template-gallery/app/src/CleanHtml.mjs' + +function TemplateGalleryEntry({ template }) { + return ( + + + + + + + {template.name} + + + + + + + + + + + ) +} + +export default memo(TemplateGalleryEntry) diff --git a/services/web/frontend/js/features/template-gallery/components/template-gallery-root.tsx b/services/web/frontend/js/features/template-gallery/components/template-gallery-root.tsx new file mode 100644 index 0000000000..d17250f33a --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/template-gallery-root.tsx @@ -0,0 +1,64 @@ +import { TemplateGalleryProvider } from '../context/template-gallery-context' +import { useTranslation } from 'react-i18next' +import useWaitForI18n from '../../../shared/hooks/use-wait-for-i18n' +import withErrorBoundary from '../../../infrastructure/error-boundary' +import { GenericErrorBoundaryFallback } from '@/shared/components/generic-error-boundary-fallback' +import getMeta from '@/utils/meta' +import DefaultNavbar from '@/features/ui/components/bootstrap-5/navbar/default-navbar' +import Footer from '@/features/ui/components/bootstrap-5/footer/footer' +import GalleryHeaderTagged from './gallery-header-tagged' +import GalleryHeaderAll from './gallery-header-all' +import TemplateGallery from './template-gallery' +import GallerySearchSortHeader from './gallery-search-sort-header' +import GalleryPopularTags from './gallery-popular-tags' + +function TemplateGalleryRoot() { + const { isReady } = useWaitForI18n() + if (!isReady) { + return null + } + return ( + + + + ) +} + +function TemplateGalleryPageContent() { + const { t } = useTranslation() + const navbarProps = getMeta('ol-navbar') + const footerProps = getMeta('ol-footer') + const category = getMeta('ol-templateCategory') + + return ( + <> + + + + {category ? ( + <> + + + > + ) : ( + <> + + + + + + {t('all_templates')} + + + > + )} + + + + > + ) +} + +export default withErrorBoundary(TemplateGalleryRoot, GenericErrorBoundaryFallback) diff --git a/services/web/frontend/js/features/template-gallery/components/template-gallery.tsx b/services/web/frontend/js/features/template-gallery/components/template-gallery.tsx new file mode 100644 index 0000000000..174fa2900e --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/components/template-gallery.tsx @@ -0,0 +1,65 @@ +import { useEffect, useState } from 'react' +import { useTranslation } from 'react-i18next' +import OLRow from '@/features/ui/components/ol/ol-row' +import { useTemplateGalleryContext } from '../context/template-gallery-context' +import TemplateGalleryEntry from './template-gallery-entry' +import Pagination from './pagination' + +export default function TemplateGallery() { + const { t } = useTranslation() + const { + searchText, + sort, + visibleTemplates, + } = useTemplateGalleryContext() + + const templatesPerPage = 6 + const totalPages = Math.ceil(visibleTemplates.length / templatesPerPage) + + const [currentPage, setCurrentPage] = useState(1) + + useEffect(() => { + setCurrentPage(1) + }, [sort]) + + const [lastNonSearchPage, setLastNonSearchPage] = useState(1) + const [isSearching, setIsSearching] = useState(false) + useEffect(() => { + if (searchText.length > 0) { + if (!isSearching) { + setLastNonSearchPage(currentPage) + setIsSearching(true) + } + setCurrentPage(1) + } else { + if (isSearching) { + setCurrentPage(lastNonSearchPage) + setIsSearching(false) + } + } + }, [searchText]) + + const startIndex = (currentPage - 1) * templatesPerPage + const currentTemplates = visibleTemplates.slice(startIndex, startIndex + templatesPerPage) + + return ( + <> + + {currentTemplates.length > 0 ? ( + currentTemplates.map(p => ( + + )) + ) : ( + + {t('no_templates_found')} + + )} + + + > + ) +} diff --git a/services/web/frontend/js/features/template-gallery/context/template-gallery-context.tsx b/services/web/frontend/js/features/template-gallery/context/template-gallery-context.tsx new file mode 100644 index 0000000000..af67af534f --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/context/template-gallery-context.tsx @@ -0,0 +1,129 @@ +import { + createContext, + ReactNode, + useContext, + useEffect, + useMemo, + useRef, + useState, +} from 'react' +import { Template } from '../../../../../types/template' +import { GetTemplatesResponseBody, Sort } from '../types/api' +import getMeta from '../../../utils/meta' +import useAsync from '../../../shared/hooks/use-async' +import { getTemplates } from '../util/api' +import sortTemplates from '../util/sort-templates' +import { debugConsole } from '@/utils/debugging' + +export type TemplateGalleryContextValue = { + visibleTemplates: Template[] + totalTemplatesCount: number + error: Error | null + sort: Sort + setSort: React.Dispatch> + searchText: string + setSearchText: React.Dispatch> +} + +export const TemplateGalleryContext = createContext< + TemplateGalleryContextValue | undefined +>(undefined) + +type TemplateGalleryProviderProps = { + children: ReactNode +} + +export function TemplateGalleryProvider({ children }: TemplateGalleryProviderProps) { + const [loadedTemplates, setLoadedTemplates] = useState([]) + const [visibleTemplates, setVisibleTemplates] = useState([]) + const [totalTemplatesCount, setTotalTemplatesCount] = useState(0) + const [sort, setSort] = useState({ + by: 'lastUpdated', + order: 'desc', + }) + const prevSortRef = useRef(sort) + + const [searchText, setSearchText] = useState('') + + const { + error, + runAsync, + } = useAsync() + + const category = getMeta('ol-templateCategory') || 'all' + + useEffect(() => { + runAsync(getTemplates(sort, category)) + .then(data => { + setLoadedTemplates(data.templates) + setTotalTemplatesCount(data.totalSize) + }) + .catch(debugConsole.error) + .finally(() => { + }) + }, [runAsync]) + + useEffect(() => { + let filteredTemplates = [...loadedTemplates] + + if (searchText.length) { + filteredTemplates = filteredTemplates.filter(template => + template.name.toLowerCase().includes(searchText.toLowerCase()) || + template.description.toLowerCase().includes(searchText.toLowerCase()) + ) + } + + if (prevSortRef.current !== sort) { + filteredTemplates = sortTemplates(filteredTemplates, sort) + const loadedTemplatesSorted = sortTemplates(loadedTemplates, sort) + setLoadedTemplates(loadedTemplatesSorted) + } + setVisibleTemplates(filteredTemplates) + }, [ + loadedTemplates, + searchText, + sort, + ]) + + useEffect(() => { + prevSortRef.current = sort + }, [sort]) + + + const value = useMemo( + () => ({ + error, + searchText, + setSearchText, + setSort, + sort, + totalTemplatesCount, + visibleTemplates, + }), + [ + error, + searchText, + setSearchText, + setSort, + sort, + totalTemplatesCount, + visibleTemplates, + ] + ) + + return ( + + {children} + + ) +} + +export function useTemplateGalleryContext() { + const context = useContext(TemplateGalleryContext) + if (!context) { + throw new Error( + 'TemplateGalleryContext is only available inside TemplateGalleryProvider' + ) + } + return context +} diff --git a/services/web/frontend/js/features/template-gallery/hooks/use-sort.ts b/services/web/frontend/js/features/template-gallery/hooks/use-sort.ts new file mode 100644 index 0000000000..675f052425 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/hooks/use-sort.ts @@ -0,0 +1,20 @@ +import { useTemplateGalleryContext } from '../context/template-gallery-context' +import { Sort } from '../types/api' +import { SortingOrder } from '../../../../../types/sorting-order' + +const toggleSort = (order: SortingOrder): SortingOrder => { + return order === 'asc' ? 'desc' : 'asc' +} + +function useSort() { + const { sort, setSort } = useTemplateGalleryContext() + const handleSort = (by: Sort['by']) => { + setSort(prev => ({ + by, + order: prev.by === by ? toggleSort(sort.order) : by === 'lastUpdated' ? 'desc' : 'asc', + })) + } + return { handleSort } +} + +export default useSort diff --git a/services/web/frontend/js/features/template-gallery/types/api.ts b/services/web/frontend/js/features/template-gallery/types/api.ts new file mode 100644 index 0000000000..d2b19807a9 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/types/api.ts @@ -0,0 +1,12 @@ +import { SortingOrder } from '../../../../../types/sorting-order' +import { Template } from '../../../../../types/template' + +export type Sort = { + by: 'lastUpdated' | 'name' + order: SortingOrder +} + +export type GetTemplatesResponseBody = { + totalSize: number + templates: Template[] +} diff --git a/services/web/frontend/js/features/template-gallery/util/api.ts b/services/web/frontend/js/features/template-gallery/util/api.ts new file mode 100644 index 0000000000..54704f187d --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/util/api.ts @@ -0,0 +1,12 @@ +import { GetTemplatesResponseBody, Sort } from '../types/api' +import { getJSON } from '../../../infrastructure/fetch-json' + +export function getTemplates(sortBy: Sort, category: string): Promise { + const queryParams = new URLSearchParams({ + by: sortBy.by, + order: sortBy.order, + category, + }).toString() + + return getJSON(`/api/templates?${queryParams}`) +} diff --git a/services/web/frontend/js/features/template-gallery/util/sort-templates.ts b/services/web/frontend/js/features/template-gallery/util/sort-templates.ts new file mode 100644 index 0000000000..2315606514 --- /dev/null +++ b/services/web/frontend/js/features/template-gallery/util/sort-templates.ts @@ -0,0 +1,40 @@ +import { Sort } from '../types/api' +import { Template } from '../../../../../types/template' +import { SortingOrder } from '../../../../../types/sorting-order' +import { Compare } from '../../../../../types/helpers/array/sort' + +const order = (order: SortingOrder, templates: Template[]) => { + return order === 'asc' ? [...templates] : templates.reverse() +} + +export const defaultComparator = ( + v1: Template, + v2: Template, + key: 'name' | 'lastUpdated' +) => { + const value1 = v1[key].toLowerCase() + const value2 = v2[key].toLowerCase() + + if (value1 !== value2) { + return value1 < value2 ? Compare.SORT_A_BEFORE_B : Compare.SORT_A_AFTER_B + } + + return Compare.SORT_KEEP_ORDER +} + +export default function sortTemplates(templates: Template[], sort: Sort) { + let sorted = [...templates] + if (sort.by === 'name') { + sorted = sorted.sort((...args) => { + return defaultComparator(...args, 'name') + }) + } + + if (sort.by === 'lastUpdated') { + sorted = sorted.sort((...args) => { + return defaultComparator(...args, 'lastUpdated') + }) + } + + return order(sort.order, sorted) +} diff --git a/services/web/frontend/js/features/template/components/delete-template-button.tsx b/services/web/frontend/js/features/template/components/delete-template-button.tsx new file mode 100644 index 0000000000..f4a17043d8 --- /dev/null +++ b/services/web/frontend/js/features/template/components/delete-template-button.tsx @@ -0,0 +1,48 @@ +import { useState } from 'react' +import { useTranslation } from 'react-i18next' +import useIsMounted from '@/shared/hooks/use-is-mounted' +import OLButton from '@/features/ui/components/ol/ol-button' +import DeleteTemplateModal from './modals/delete-template-modal' +import { useTemplateContext } from '../context/template-context' +import { deleteTemplate } from '../util/api' +import type { Template } from '../../../../../types/template' + +function DeleteTemplateButton() { + const { t } = useTranslation() + const [showModal, setShowModal] = useState(false) + const isMounted = useIsMounted() + const { template, setTemplate } = useTemplateContext() + + const handleOpenModal = () => { + setShowModal(true) + } + + const handleCloseModal = () => { + if (isMounted.current) { + setShowModal(false) + } + } + + const handleDeleteTemplate = async (template: Template) => { + await deleteTemplate(template) + handleCloseModal() + const previousPage = document.referrer || '/templates' + window.location.href = previousPage + } + + return ( + <> + + {t('delete')} + + + > + ) +} + +export default DeleteTemplateButton diff --git a/services/web/frontend/js/features/template/components/edit-template-button.tsx b/services/web/frontend/js/features/template/components/edit-template-button.tsx new file mode 100644 index 0000000000..ee1bd92df9 --- /dev/null +++ b/services/web/frontend/js/features/template/components/edit-template-button.tsx @@ -0,0 +1,46 @@ +import { useState } from 'react' +import { useTranslation } from 'react-i18next' +import useIsMounted from '@/shared/hooks/use-is-mounted' +import OLButton from '@/features/ui/components/ol/ol-button' +import EditTemplateModal from './modals/edit-template-modal' +import { useTemplateContext } from '../context/template-context' +import { updateTemplate } from '../util/api' +import type { Template } from '../../../../../types/template' + +export default function EditTemplateButton() { + const { t } = useTranslation() + const [showModal, setShowModal] = useState(false) + const isMounted = useIsMounted() + const { template, setTemplate } = useTemplateContext() + + const handleOpenModal = () => { + setShowModal(true) + } + + const handleCloseModal = () => { + if (isMounted.current) { + setShowModal(false) + } + } + + const handleEditTemplate = async (editedTemplate: Template) => { + const updated = await updateTemplate({ editedTemplate, template }) + if (updated) { + setTemplate(prev => ({ ...prev, ...updated })) + } + } + + return ( + <> + + {t('edit')} + + + + > + ) +} diff --git a/services/web/frontend/js/features/template/components/form/form-field-input.tsx b/services/web/frontend/js/features/template/components/form/form-field-input.tsx new file mode 100644 index 0000000000..d99a19ff8c --- /dev/null +++ b/services/web/frontend/js/features/template/components/form/form-field-input.tsx @@ -0,0 +1,17 @@ +import React from 'react' +import OLFormControl from '@/features/ui/components/ol/ol-form-control' + +interface FormFieldInputProps extends React.ComponentProps { + value: string + placeholder?: string + onChange: React.ChangeEventHandler +} + +const FormFieldInput: React.FC = ({ + type = 'text', + ...props +}) => ( + +) + +export default FormFieldInput diff --git a/services/web/frontend/js/features/template/components/form/labeled-row-form-group.tsx b/services/web/frontend/js/features/template/components/form/labeled-row-form-group.tsx new file mode 100644 index 0000000000..49eea6eeab --- /dev/null +++ b/services/web/frontend/js/features/template/components/form/labeled-row-form-group.tsx @@ -0,0 +1,26 @@ +import React from 'react' +import OLFormGroup from '@/features/ui/components/ol/ol-form-group' +import OLFormLabel from '@/features/ui/components/ol/ol-form-label' + +interface LabeledRowFormGroupProps { + controlId: string + label: string + children: React.ReactNode +} + +const LabeledRowFormGroup: React.FC = ({ + controlId, + label, + children, +}) => ( + + + {label} + + + {children} + + +) + +export default React.memo(LabeledRowFormGroup) diff --git a/services/web/frontend/js/features/template/components/form/template-form-fields.tsx b/services/web/frontend/js/features/template/components/form/template-form-fields.tsx new file mode 100644 index 0000000000..f7d110e53f --- /dev/null +++ b/services/web/frontend/js/features/template/components/form/template-form-fields.tsx @@ -0,0 +1,96 @@ +import React, { useCallback } from 'react' +import LabeledRowFormGroup from '../form/labeled-row-form-group' +import FormFieldInput from '../form/form-field-input' +import SettingsTemplateCategory from '../settings/settings-template-category' +import SettingsLicense from '../settings/settings-license' +import SettingsLanguage from '../settings/settings-language' +import { useTranslation } from 'react-i18next' +import type { Template } from '../../../../../../types/template' + +interface TemplateFormFieldsProps { + template: Partial + includeLanguage?: boolean + onChange: (changes: Partial) => void + onEnterKey?: () => void +} + +function TemplateFormFields({ + template, + includeLanguage = false, + onChange, + onEnterKey, +}: TemplateFormFieldsProps) { + const { t } = useTranslation() + + const handleKeyDown = useCallback( + (e: React.KeyboardEvent) => { + if (e.key === 'Enter') { + e.preventDefault() + onEnterKey?.() + } + }, + [onEnterKey] + ) + + return ( + <> + + onChange({ name: e.target.value })} + onKeyDown={handleKeyDown} + /> + + + + onChange({ authorMD: e.target.value })} + onKeyDown={handleKeyDown} + /> + + + + onChange({ category: val })} + /> + + + + onChange({ descriptionMD: e.target.value })} + autoFocus + /> + + + + onChange({ license: val })} + /> + + + {includeLanguage && ( + + onChange({ language: val })} + /> + + )} + > + ) +} + +export default React.memo(TemplateFormFields) diff --git a/services/web/frontend/js/features/template/components/manage-template-modal/editor-manage-template-modal-wrapper.tsx b/services/web/frontend/js/features/template/components/manage-template-modal/editor-manage-template-modal-wrapper.tsx new file mode 100644 index 0000000000..bb40b64f51 --- /dev/null +++ b/services/web/frontend/js/features/template/components/manage-template-modal/editor-manage-template-modal-wrapper.tsx @@ -0,0 +1,40 @@ +import React from 'react' +import withErrorBoundary from '@/infrastructure/error-boundary' +import { useProjectContext } from '@/shared/context/project-context' +import ManageTemplateModal from './manage-template-modal' +import type { Template } from '../../../../../../types/template' + +interface EditorManageTemplateModalWrapperProps { + show: boolean + handleHide: () => void + openTemplate: (data: Template) => void +} + +const EditorManageTemplateModalWrapper = React.memo( + function EditorManageTemplateModalWrapper({ + show, + handleHide, + openTemplate, + }: EditorManageTemplateModalWrapperProps) { + const { + _id: projectId, + name: projectName, + } = useProjectContext() + + if (!projectName) { + // wait for useProjectContext + return null + } + return ( + + ) + } +) + +export default withErrorBoundary(EditorManageTemplateModalWrapper) diff --git a/services/web/frontend/js/features/template/components/manage-template-modal/manage-template-modal-content.tsx b/services/web/frontend/js/features/template/components/manage-template-modal/manage-template-modal-content.tsx new file mode 100644 index 0000000000..46bc7fcf46 --- /dev/null +++ b/services/web/frontend/js/features/template/components/manage-template-modal/manage-template-modal-content.tsx @@ -0,0 +1,169 @@ +import React, { useEffect, useState, useRef } from 'react' +import { useTranslation } from 'react-i18next' +import { debugConsole } from '@/utils/debugging' +import { getJSON, postJSON } from '@/infrastructure/fetch-json' +import Notification from '@/shared/components/notification' +import { + OLModalBody, + OLModalFooter, + OLModalHeader, + OLModalTitle, +} from '@/features/ui/components/ol/ol-modal' +import OLForm from '@/features/ui/components/ol/ol-form' +import OLButton from '@/features/ui/components/ol/ol-button' +import { useDetachCompileContext } from '@/shared/context/detach-compile-context' +import { useUserContext } from '@/shared/context/user-context' +import { useFocusTrap } from '../../hooks/use-focus-trap' +import TemplateFormFields from '../form/template-form-fields' +import type { Template } from '../../../../../../types/template' + + +interface ManageTemplateModalContentProps { + handleHide: () => void + inFlight: boolean + setInFlight: (inFlight: boolean) => void + handleAfterPublished: (data: Template) => void + projectId: string + projectName: string +} + +export default function ManageTemplateModalContent({ + handleHide, + inFlight, + setInFlight, + handleAfterPublished, + projectId, + projectName, +}: ManageTemplateModalContentProps) { + const { t } = useTranslation() + const { pdfFile } = useDetachCompileContext() + const user = useUserContext() + + const [template, setTemplate] = useState>({ + name: projectName, + authorMD: `${user.first_name} ${user.last_name}`.trim(), + }) + const [override, setOverride] = useState(false) + const [titleConflict, setTitleConflict] = useState(false) + const [error, setError] = useState(false) + const [notificationType, setNotificationType] = useState<'error' | 'warning'>('error') + const [disablePublish, setDisablePublish] = useState(false) + + // Only the trimmed name gates submission + const valid = (template.name ?? '').trim() + + useEffect(() => { + const queryParams = new URLSearchParams({ key: 'name', val: projectName }) + getJSON(`/api/template?${queryParams}`) + .then((data) => { + if (!data) return + setTemplate(prev => ({ + ...prev, + descriptionMD: data.descriptionMD, + authorMD: data.authorMD, + license: data.license, + category: data.category, + })) + }) + .catch(debugConsole.error) + }, []) + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault() + + if (!valid) return + + setError(false) + setInFlight(true) + + postJSON(`/template/new/${projectId}`, { + body: { + category: template.category, + name: valid, + authorMD: (template.authorMD ?? '').trim(), + license: template.license, + descriptionMD: (template.descriptionMD ?? '').trim(), + build: pdfFile.build, + override, + }, + }) + .then(data => { + handleHide() + handleAfterPublished(data) + }) + .catch(({ response, data }) => { + if (response?.status === 409 && data.canOverride) { + setNotificationType('warning') + setOverride(true) + } else { + setNotificationType('error') + setDisablePublish(true) + } + setError(data.message) + if (response?.status === 409) setTitleConflict(true) + }) + .finally(() => { + setInFlight(false) + }) + } + + const handleChange = (changes: Partial) => { + if ('name' in changes && titleConflict) { + setError(false) + setOverride(false) + if (disablePublish) setDisablePublish(false) + } + setTemplate(prev => ({ ...prev, ...changes })) + } + + const handleEnterKey = () => { + document.getElementById('submit-publish-template')?.click() + } + + const modalRef = useRef(null) + useFocusTrap(modalRef) + + return ( + + + {t('publish_as_template')} + + + + + + + + + + + {error && ( + + )} + + + + + {t('cancel')} + + + {inFlight ? <>{t('publishing')}…> : override ? t('overwrite') : t('publish')} + + + + ) +} diff --git a/services/web/frontend/js/features/template/components/manage-template-modal/manage-template-modal.tsx b/services/web/frontend/js/features/template/components/manage-template-modal/manage-template-modal.tsx new file mode 100644 index 0000000000..c48980c598 --- /dev/null +++ b/services/web/frontend/js/features/template/components/manage-template-modal/manage-template-modal.tsx @@ -0,0 +1,52 @@ +import React, { memo, useCallback, useState } from 'react' +import OLModal from '@/features/ui/components/ol/ol-modal' +import ManageTemplateModalContent from './manage-template-modal-content' +import type { Template } from '../../../../../../types/template' + +interface ManageTemplateModalProps { + show: boolean + handleHide: () => void + handleAfterPublished: (data: Template) => void + projectId: string + projectName: string +} + +function ManageTemplateModal({ + show, + handleHide, + handleAfterPublished, + projectId, + projectName, +}: ManageTemplateModalProps) { + const [inFlight, setInFlight] = useState(false) + + const onHide = useCallback(() => { + if (!inFlight) { + handleHide() + } + }, [handleHide, inFlight]) + + return ( + + + + ) +} + +export default memo(ManageTemplateModal) diff --git a/services/web/frontend/js/features/template/components/modals/delete-template-modal.tsx b/services/web/frontend/js/features/template/components/modals/delete-template-modal.tsx new file mode 100644 index 0000000000..8588703c30 --- /dev/null +++ b/services/web/frontend/js/features/template/components/modals/delete-template-modal.tsx @@ -0,0 +1,43 @@ +import { useTranslation } from 'react-i18next' +import withErrorBoundary from '@/infrastructure/error-boundary' +import Notification from '@/shared/components/notification' +import TemplateActionModal from './template-action-modal' + +type DeleteTemplateModalProps = Pick< + React.ComponentProps, + 'template' | 'actionHandler' | 'showModal' | 'handleCloseModal' +> + +function DeleteTemplateModal({ + template, + actionHandler, + showModal, + handleCloseModal, +}: DeleteTemplateModalProps) { + const { t } = useTranslation() + + return ( + + {t('about_to_delete_template')} + + + {template.name} + + + + + + ) +} + +export default withErrorBoundary(DeleteTemplateModal) diff --git a/services/web/frontend/js/features/template/components/modals/edit-template-modal.tsx b/services/web/frontend/js/features/template/components/modals/edit-template-modal.tsx new file mode 100644 index 0000000000..3c7e8d274a --- /dev/null +++ b/services/web/frontend/js/features/template/components/modals/edit-template-modal.tsx @@ -0,0 +1,139 @@ +import React, { useReducer, useState, useEffect, useRef, useCallback, useMemo } from 'react' +import { useTranslation } from 'react-i18next' +import OLForm from '@/features/ui/components/ol/ol-form' +import OLButton from '@/features/ui/components/ol/ol-button' +import withErrorBoundary from '@/infrastructure/error-boundary' +//import { useFocusTrap } from '../../hooks/use-focus-trap' +import TemplateActionModal from './template-action-modal' +import { useTemplateContext } from '../../context/template-context' +import TemplateFormFields from '../form/template-form-fields' +import type { Template } from '../../../../../../types/template' + +type EditTemplateModalProps = { + showModal: boolean + handleCloseModal: () => void + actionHandler: (editedTemplate: Template) => void | Promise +} + +type ActionError = { + info?: { + statusCode?: number + } +} + +type TemplateFormAction = + | { type: 'UPDATE'; payload: Partial } + | { type: 'RESET'; payload: Template } + | { type: 'CLEAR_FIELD'; field: keyof Template } + +function templateFormReducer(state: Template, action: TemplateFormAction): Template { + switch (action.type) { + case 'UPDATE': + return { ...state, ...action.payload } + case 'RESET': + return { ...action.payload } + case 'CLEAR_FIELD': + return { ...state, [action.field]: '' } + default: + return state + } +} + +function EditTemplateModal({ + showModal, + handleCloseModal, + actionHandler, +}: EditTemplateModalProps) { + const { t } = useTranslation() + const { template } = useTemplateContext() + + const [editedTemplate, dispatch] = useReducer(templateFormReducer, template) + const [actionError, setActionError] = useState(null) + const clearModalErrorRef = useRef<() => void>(() => {}) + + useEffect(() => { + if (showModal) { + dispatch({ type: 'RESET', payload: template }) + setActionError(null) + } + }, [showModal, template]) + + const isConflictError = useMemo( + () => actionError?.info?.statusCode === 409, + [actionError] + ) + + const valid = useMemo( + () => editedTemplate.name.trim().length > 0, + [editedTemplate.name] + ) + + const handleChange = useCallback( + (changes: Partial) => { + dispatch({ type: 'UPDATE', payload: changes }) + if ('name' in changes && isConflictError) { + setActionError(null) + clearModalErrorRef.current?.() + } + }, + [isConflictError] + ) + + const handleEnterKey = useCallback(() => { + document.getElementById('submit-edit-template')?.click() + }, []) + + const handleAction = useCallback(() => { + return Promise.resolve(actionHandler(editedTemplate)).catch(err => { + setActionError(err) + throw err + }) + }, [actionHandler, editedTemplate]) + + const submitButtonDisabled = !valid || isConflictError + + return ( + ( + <> + + {t('cancel')} + + + {t('save')} + + > + )} + onClearError={fn => { + clearModalErrorRef.current = fn + }} + > + + + e.preventDefault()}> + + + + + + ) +} + +export default withErrorBoundary(React.memo(EditTemplateModal)) diff --git a/services/web/frontend/js/features/template/components/modals/template-action-modal.tsx b/services/web/frontend/js/features/template/components/modals/template-action-modal.tsx new file mode 100644 index 0000000000..24c78a0e29 --- /dev/null +++ b/services/web/frontend/js/features/template/components/modals/template-action-modal.tsx @@ -0,0 +1,145 @@ +import { memo, useEffect, useState, useRef } from 'react' +import { useTranslation } from 'react-i18next' +import { getUserFacingMessage } from '@/infrastructure/fetch-json' +import * as eventTracking from '@/infrastructure/event-tracking' +import { isSmallDevice } from '@/infrastructure/event-tracking' +import useIsMounted from '@/shared/hooks/use-is-mounted' +import Notification from '@/shared/components/notification' +import OLButton from '@/features/ui/components/ol/ol-button' +import OLModal, { + OLModalBody, + OLModalFooter, + OLModalHeader, + OLModalTitle, +} from '@/features/ui/components/ol/ol-modal' +import type { Template } from '../../../../../../types/template' +import { useFocusTrap } from '../../hooks/use-focus-trap' + +type TemplateActionModalProps = { + title: string + size?: string + action: 'delete' | 'edit' + actionHandler: (template: Template) => Promise + handleCloseModal: () => void + template: Template + showModal: boolean + children?: React.ReactNode + renderFooterButtons?: (props: { + onConfirm: () => void + onCancel: () => void + isProcessing: boolean + }) => React.ReactNode + onClearError?: (clear: () => void) => void +} + +function TemplateActionModal({ + title, + size, + action, + actionHandler, + handleCloseModal, + showModal, + template, + children, + renderFooterButtons, + onClearError, +}: TemplateActionModalProps) { + const { t } = useTranslation() + const [error, setError] = useState(false) + const [isProcessing, setIsProcessing] = useState(false) + const isMounted = useIsMounted() + const modalRef = useRef(null) + + useFocusTrap(modalRef, showModal) + + useEffect(() => { + if (onClearError) { + onClearError(() => setError(false)) + } + }, [onClearError]) + + async function handleActionForTemplate(template: Template) { + let errored + setIsProcessing(true) + setError(false) + + try { + await actionHandler(template) + } catch (e) { + errored = { name: template.name, error: e } + } + + if (isMounted.current) { + setIsProcessing(false) + } + + if (!errored) { + handleCloseModal() + } else { + setError(errored) + } + } + + useEffect(() => { + if (showModal) { + eventTracking.sendMB('template-info-page-interaction', { + action, + isSmallDevice, + }) + } else { + setError(false) + } + }, [action, showModal]) + + return ( + + + + {title} + + + + {children} + {!isProcessing && error && ( + + )} + + + + {renderFooterButtons ? ( + renderFooterButtons({ + onConfirm: () => handleActionForTemplate(template), + onCancel: handleCloseModal, + isProcessing, + }) + ) : ( + <> + + {t('cancel')} + + handleActionForTemplate(template)} + disabled={isProcessing} + > + {t('confirm')} + + > + )} + + + + ) +} + +export default memo(TemplateActionModal) diff --git a/services/web/frontend/js/features/template/components/settings/settings-language.tsx b/services/web/frontend/js/features/template/components/settings/settings-language.tsx new file mode 100644 index 0000000000..e9b7929617 --- /dev/null +++ b/services/web/frontend/js/features/template/components/settings/settings-language.tsx @@ -0,0 +1,42 @@ +import { useMemo } from 'react' +import { useTranslation } from 'react-i18next' +import getMeta from '../../../../utils/meta' +import SettingsMenuSelect from './settings-menu-select' +import type { Optgroup } from './settings-menu-select' + +interface SettingsLanguageProps { + value: string + onChange: (value: string) => void +} + +export default function SettingsLanguage({ + value, + onChange, +}: SettingsLanguageProps) { + const { t } = useTranslation() + + const optgroup: Optgroup = useMemo(() => { + const options = (getMeta('ol-languages') ?? []) + // only include spell-check languages that are available in the client + .filter(language => language.dic !== undefined) + + return { + label: 'Language', + options: options.map(language => ({ + value: language.code, + label: language.name, + })), + } + }, []) + + return ( + + ) +} diff --git a/services/web/frontend/js/features/template/components/settings/settings-license.tsx b/services/web/frontend/js/features/template/components/settings/settings-license.tsx new file mode 100644 index 0000000000..5b72d81b93 --- /dev/null +++ b/services/web/frontend/js/features/template/components/settings/settings-license.tsx @@ -0,0 +1,33 @@ +import { useTranslation } from 'react-i18next' +import SettingsMenuSelect from './settings-menu-select' +import type { Option } from './settings-menu-select' + +export const licensesMap = { + 'cc_by_4.0': 'Creative Commons CC BY 4.0', + 'lppl_1.3c': 'LaTeX Project Public License 1.3c', + 'other': 'Other (as stated in the work)', +} + +interface SettingsLicenseProps { + value: string + onChange: (value: string) => void +} + +export default function SettingsLicense({ + value, + onChange, +}: SettingsLicenseProps) { + const { t } = useTranslation() + + const options = Object.entries(licensesMap).map(([value, label]) => ({ value, label })) + + return ( + + ) +} diff --git a/services/web/frontend/js/features/template/components/settings/settings-menu-select.tsx b/services/web/frontend/js/features/template/components/settings/settings-menu-select.tsx new file mode 100644 index 0000000000..b1fa62226c --- /dev/null +++ b/services/web/frontend/js/features/template/components/settings/settings-menu-select.tsx @@ -0,0 +1,91 @@ +import { ChangeEventHandler, useCallback, useRef, useEffect } from 'react' +import OLFormGroup from '@/features/ui/components/ol/ol-form-group' +import OLFormLabel from '@/features/ui/components/ol/ol-form-label' +import OLFormSelect from '@/features/ui/components/ol/ol-form-select' + +type PossibleValue = string | number | boolean + +export type Option = { + value: T + label: string + ariaHidden?: 'true' | 'false' + disabled?: boolean +} + +export type Optgroup = { + label: string + options: Array> +} + +type SettingsMenuSelectProps = { + name: string + options: Array> + optgroup?: Optgroup + onChange: (val: T) => void + value?: T + disabled?: boolean +} + +export default function SettingsMenuSelect( + props: SettingsMenuSelectProps +) { + + const { name, options, optgroup, onChange, value, disabled = false } = props + const defaultApplied = useRef(false) + + useEffect(() => { + if (value === undefined || value === null) { + onChange(options?.[0]?.value || optgroup?.options?.[0]?.value) + } + }, [value, options, onChange]) + + const handleChange: ChangeEventHandler = useCallback( + event => { + const selectedValue = event.target.value + let onChangeValue: PossibleValue = selectedValue + if (typeof value === 'boolean') { + onChangeValue = selectedValue === 'true' + } else if (typeof value === 'number') { + onChangeValue = parseInt(selectedValue, 10) + } + onChange(onChangeValue as T) + }, + [onChange, value] + ) + const selectRef = useRef(null) + + return ( + <> + + + {options.map(option => ( + + {option.label} + + ))} + {optgroup ? ( + + {optgroup.options.map(option => ( + + {option.label} + + ))} + + ) : null} + + > + ) +} diff --git a/services/web/frontend/js/features/template/components/settings/settings-template-category.tsx b/services/web/frontend/js/features/template/components/settings/settings-template-category.tsx new file mode 100644 index 0000000000..d886ce61e3 --- /dev/null +++ b/services/web/frontend/js/features/template/components/settings/settings-template-category.tsx @@ -0,0 +1,44 @@ +import React, { useMemo } from 'react' +import { useTranslation } from 'react-i18next' +import getMeta from '@/utils/meta' +import SettingsMenuSelect from './settings-menu-select' +import type { Option } from './settings-menu-select' + +interface SettingsTemplateCategoryProps { + value: string + onChange: (value: string) => void +} + +const SettingsTemplateCategory: React.FC = ({ + value, + onChange, +}) => { + const { t } = useTranslation() + + const options: Option[] = useMemo(() => { + const { templateLinks = [] } = getMeta('ol-ExposedSettings') as { + templateLinks?: Array<{ name: string; url: string; description: string }> + } + + return templateLinks.map(({ name, url }) => ({ + value: url, + label: name, + })) + }, []) + + if (options.length === 0) { + return null + } + + return ( + + ) +} + +export default React.memo(SettingsTemplateCategory) diff --git a/services/web/frontend/js/features/template/components/template-details.tsx b/services/web/frontend/js/features/template/components/template-details.tsx new file mode 100644 index 0000000000..6c1a9b1059 --- /dev/null +++ b/services/web/frontend/js/features/template/components/template-details.tsx @@ -0,0 +1,104 @@ +import { useTranslation } from 'react-i18next' +import getMeta from '@/utils/meta' +import OLCol from '@/features/ui/components/ol/ol-col' +import OLRow from '@/features/ui/components/ol/ol-row' +import OLTooltip from '@/features/ui/components/ol/ol-tooltip' +import { formatDate, fromNowDate } from '../../../utils/dates' +import { cleanHtml } from '../../../../../modules/template-gallery/app/src/CleanHtml.mjs' +import { useTemplateContext } from '../context/template-context' +import DeleteTemplateButton from './delete-template-button' +import EditTemplateButton from './edit-template-button' +import { licensesMap } from './settings/settings-license' + +function TemplateDetails() { + const { t } = useTranslation() + const {template, setTemplate} = useTemplateContext() + const lastUpdatedDate = fromNowDate(template.lastUpdated) + const tooltipText = formatDate(template.lastUpdated) + const loggedInUserId = getMeta('ol-user_id') + const loggedInUserIsAdmin = getMeta('ol-userIsAdmin') + + const openAsTemplateParams = new URLSearchParams({ + version: template.version, + ...(template.brandVariationId && { brandVariationId: template.brandVariationId }), + name: template.name, + compiler: template.compiler, + mainFile: template.mainFile, + language: template.language, + ...(template.imageName && { imageName: template.imageName }) + }).toString() + + const sanitizedAuthor = cleanHtml(template.author, 'linksOnly') || t('anonymous') + const sanitizedDescription = cleanHtml(template.description, 'reachText') + + return ( + <> + + + + {template.name} + + + + + + {t('open_as_template')} + {t('view_pdf')} + + + + + + {t('author')}: + + + + + + {t('last_updated')}: + + + + + {lastUpdatedDate.trim()} + + + + + + + {t('license')}: + + + {licensesMap[template.license]} + + + {sanitizedDescription && ( + + + {t('abstract')}: + + + + + )} + + {loggedInUserId && (loggedInUserId === template.owner || loggedInUserIsAdmin) && ( + + + + + + + )} + > + ) +} +export default TemplateDetails diff --git a/services/web/frontend/js/features/template/components/template-preview.tsx b/services/web/frontend/js/features/template/components/template-preview.tsx new file mode 100644 index 0000000000..547c86159d --- /dev/null +++ b/services/web/frontend/js/features/template/components/template-preview.tsx @@ -0,0 +1,23 @@ +import OLCol from '@/features/ui/components/ol/ol-col' +import OLRow from '@/features/ui/components/ol/ol-row' +import { useTemplateContext } from '../context/template-context' + + +function TemplatePreview() { + const { template, setTemplate } = useTemplateContext() + return ( + + + + + + + + + + ) +} +export default TemplatePreview diff --git a/services/web/frontend/js/features/template/components/template-root.tsx b/services/web/frontend/js/features/template/components/template-root.tsx new file mode 100644 index 0000000000..ff15511202 --- /dev/null +++ b/services/web/frontend/js/features/template/components/template-root.tsx @@ -0,0 +1,70 @@ +import { useTranslation } from 'react-i18next' +import useWaitForI18n from '../../../shared/hooks/use-wait-for-i18n' +import withErrorBoundary from '../../../infrastructure/error-boundary' +import { GenericErrorBoundaryFallback } from '@/shared/components/generic-error-boundary-fallback' +import DefaultNavbar from '@/features/ui/components/bootstrap-5/navbar/default-navbar' +import Footer from '@/features/ui/components/bootstrap-5/footer/footer' +import getMeta from '@/utils/meta' +import OLCol from '@/features/ui/components/ol/ol-col' +import OLRow from '@/features/ui/components/ol/ol-row' +import TemplateDetails from './template-details' +import TemplatePreview from './template-preview' +import { useTemplateContext, TemplateProvider } from '../context/template-context' + +function TemplateRoot() { + const { isReady } = useWaitForI18n() + if (!isReady) { + return null + } + return ( + + + + ) +} + +function TemplatePageContent() { + const { t } = useTranslation() + const navbarProps = getMeta('ol-navbar') + const footerProps = getMeta('ol-footer') + const { template } = useTemplateContext() + const { templateLinks } = getMeta('ol-ExposedSettings') || [] + const categoryName = templateLinks?.find(link => link.url === template.category)?.name + + return ( + <> + + + + + + + arrow_left_alt + {t('all_templates')} + + {categoryName && template.category !== '/templates/all' && ( + <> + / + + {categoryName} + + > + )} + + + + + + + + + + + + + + > + ) +} + +export default withErrorBoundary(TemplateRoot, GenericErrorBoundaryFallback) diff --git a/services/web/frontend/js/features/template/context/template-context.tsx b/services/web/frontend/js/features/template/context/template-context.tsx new file mode 100644 index 0000000000..06c28604a8 --- /dev/null +++ b/services/web/frontend/js/features/template/context/template-context.tsx @@ -0,0 +1,53 @@ +import { + createContext, + FC, + useCallback, + useContext, + useState, + useMemo, +} from 'react' +import useEventListener from '@/shared/hooks/use-event-listener' +import getMeta from '@/utils/meta' +import { Template } from '../../../../../types/template' + +type TemplateContextType = { + template: Template + setTemplate: (template: Template) => void +} + +export const TemplateContext = createContext( + undefined +) + +type TemplateProviderProps = { + loadedTemplate: Template +} + +export const TemplateProvider: FC = ({ children }) => { + const loadedTemplate = useMemo(() => getMeta('ol-template'), []) + const [template, setTemplate] = useState(loadedTemplate) + + const value = useMemo( + () => ({ + template, + setTemplate, + }), + [template, setTemplate] + ) + + return ( + + {children} + + ) +} + +export const useTemplateContext = () => { + const context = useContext(TemplateContext) + if (!context) { + throw new Error( + `useTemplateContext must be used within a TemplateProvider` + ) + } + return context +} diff --git a/services/web/frontend/js/features/template/hooks/use-focus-trap.ts b/services/web/frontend/js/features/template/hooks/use-focus-trap.ts new file mode 100644 index 0000000000..fa4e78570e --- /dev/null +++ b/services/web/frontend/js/features/template/hooks/use-focus-trap.ts @@ -0,0 +1,46 @@ +import { useEffect } from 'react' + +export function useFocusTrap(ref: React.RefObject, enabled = true) { + useEffect(() => { + if (!enabled || !ref.current) return + + const element = ref.current + const previouslyFocusedElement = document.activeElement as HTMLElement + const focusableElements = element.querySelectorAll( + 'a[href], button, textarea, input, select, [tabindex]:not([tabindex="-1"])' + ) + + const firstElement = focusableElements[0] + const lastElement = focusableElements[focusableElements.length - 1] + + // Don't override if something inside already received focus + const isAlreadyFocusedInside = element.contains(document.activeElement) + + function handleKeyDown(e: KeyboardEvent) { + if (e.key !== 'Tab') return + + if (e.shiftKey) { + if (document.activeElement === firstElement) { + e.preventDefault() + lastElement?.focus() + } + } else { + if (document.activeElement === lastElement) { + e.preventDefault() + firstElement?.focus() + } + } + } + + element.addEventListener('keydown', handleKeyDown) + + if (!isAlreadyFocusedInside) { + firstElement?.focus() + } + + return () => { + element.removeEventListener('keydown', handleKeyDown) + previouslyFocusedElement?.focus() + } + }, [ref, enabled]) +} diff --git a/services/web/frontend/js/features/template/util/api.ts b/services/web/frontend/js/features/template/util/api.ts new file mode 100644 index 0000000000..bdf4f57174 --- /dev/null +++ b/services/web/frontend/js/features/template/util/api.ts @@ -0,0 +1,47 @@ +import { deleteJSON, postJSON } from '@/infrastructure/fetch-json' +import { Template } from '../../../../../types/template' + +export function deleteTemplate(template: Template) { + return deleteJSON(`/template/${template.id}/delete`, { + body: { + version: template.version, + }, + }) +} + +type UpdateTemplateOptions = { + template: Template + initialTemplate: Template + descriptionEdited: boolean +} + +export function updateTemplate({ + editedTemplate, + template +}: UpdateTemplateOptions): Promise { + const updatedFields: Partial = { + name: editedTemplate.name.trim(), + license: editedTemplate.license.trim(), + category: editedTemplate.category, + language: editedTemplate.language, + authorMD: editedTemplate.authorMD.trim(), + descriptionMD: editedTemplate.descriptionMD.trim(), + } + + const changedFields = Object.entries(updatedFields).reduce((diff, [key, value]) => { + if (value !== undefined && template[key as keyof Template] !== value) { + diff[key] = value + } + return diff + }, {} as Partial) + + if (Object.keys(changedFields).length === 0) { + return null + } + + const updated = postJSON(`/template/${editedTemplate.id}/edit`, { + body: changedFields + }) + + return updated +} diff --git a/services/web/frontend/js/features/ui/components/bootstrap-5/navbar/logged-in-items.tsx b/services/web/frontend/js/features/ui/components/bootstrap-5/navbar/logged-in-items.tsx index 5273eb8e98..7df9da710e 100644 --- a/services/web/frontend/js/features/ui/components/bootstrap-5/navbar/logged-in-items.tsx +++ b/services/web/frontend/js/features/ui/components/bootstrap-5/navbar/logged-in-items.tsx @@ -4,6 +4,7 @@ import type { NavbarSessionUser } from '@/features/ui/components/types/navbar' import NavLinkItem from '@/features/ui/components/bootstrap-5/navbar/nav-link-item' import { AccountMenuItems } from './account-menu-items' import { useSendProjectListMB } from '@/features/project-list/components/project-list-events' +import getMeta from '@/utils/meta' export default function LoggedInItems({ sessionUser, @@ -14,11 +15,18 @@ export default function LoggedInItems({ }) { const { t } = useTranslation() const sendProjectListMB = useSendProjectListMB() + const { templatesEnabled } = getMeta('ol-ExposedSettings') + return ( <> {t('projects')} + {templatesEnabled && ( + + {t('templates')} + + )} + {templatesEnabled && ( + + {t('templates')} + + )} {showSignUpLink ? ( ) +} diff --git a/services/web/frontend/js/pages/template.tsx b/services/web/frontend/js/pages/template.tsx new file mode 100644 index 0000000000..02d29d04da --- /dev/null +++ b/services/web/frontend/js/pages/template.tsx @@ -0,0 +1,15 @@ +import './../utils/meta' +import '../utils/webpack-public-path' +import './../infrastructure/error-reporter' +import '@/i18n' +import '../features/event-tracking' +import '../features/cookie-banner' +import '../features/link-helpers/slow-link' +import ReactDOM from 'react-dom/client' +import TemplateRoot from '../features/template/components/template-root' + +const element = document.getElementById('template-root') +if (element) { + const root = ReactDOM.createRoot(element) + root.render() +} diff --git a/services/web/frontend/js/utils/meta.ts b/services/web/frontend/js/utils/meta.ts index 9461635625..110a70907a 100644 --- a/services/web/frontend/js/utils/meta.ts +++ b/services/web/frontend/js/utils/meta.ts @@ -24,6 +24,7 @@ import { } from '../../../types/project/dashboard/notification' import { Survey } from '../../../types/project/dashboard/survey' import { GetProjectsResponseBody } from '../../../types/project/dashboard/api' +import { GetTemplatesResponseBody } from '../../../types/template/dashboard/api' import { Tag } from '../../../app/src/Features/Tags/types' import { Institution } from '../../../types/institution' import { @@ -184,6 +185,7 @@ export interface Meta { 'ol-postCheckoutRedirect': string 'ol-postUrl': string 'ol-prefetchedProjectsBlob': GetProjectsResponseBody | undefined + 'ol-prefetchedTemplatesBlob': GetTemplatesResponseBody | undefined 'ol-preventCompileOnLoad'?: boolean 'ol-primaryEmail': { email: string; confirmed: boolean } 'ol-project': any // TODO diff --git a/services/web/frontend/stylesheets/bootstrap-5/components/link.scss b/services/web/frontend/stylesheets/bootstrap-5/components/link.scss index 7a6b96b597..66174eedc5 100644 --- a/services/web/frontend/stylesheets/bootstrap-5/components/link.scss +++ b/services/web/frontend/stylesheets/bootstrap-5/components/link.scss @@ -9,5 +9,6 @@ i { margin-right: var(--spacing-02); padding-bottom: 3px; + vertical-align: middle; } } diff --git a/services/web/frontend/stylesheets/bootstrap-5/pages/templates-v2.scss b/services/web/frontend/stylesheets/bootstrap-5/pages/templates-v2.scss index a829bb80d5..3722922fb0 100644 --- a/services/web/frontend/stylesheets/bootstrap-5/pages/templates-v2.scss +++ b/services/web/frontend/stylesheets/bootstrap-5/pages/templates-v2.scss @@ -23,6 +23,37 @@ .gallery { padding-top: calc($header-height + var(--spacing-10)) !important; + .gallery-search-form-control { + padding-top: 0px; + padding-bottom: 0px; + } + + + .gallery-header-sort-btn { + font-size: var(--font-size-02); + border: 0; + text-align: left; + color: var(--neutral-90); + background-color: transparent; + padding: 0; + font-weight: bold; + max-width: 100%; + overflow: hidden; + text-overflow: ellipsis; + text-decoration: none; + + &:hover { + color: var(--neutral-90); + text-decoration: underline; + } + + .material-symbols { + vertical-align: top; + font-size: var(--font-size-05); + } + } + + .gallery-tagged-tags-container-spacing { padding: 0 var(--spacing-09); margin-bottom: var(--spacing-16); diff --git a/services/web/locales/de.json b/services/web/locales/de.json index 11129073df..0957e28520 100644 --- a/services/web/locales/de.json +++ b/services/web/locales/de.json @@ -19,6 +19,7 @@ "about_to_delete_cert": "Du bist dabei, das folgende Zertifikat zu löschen:", "about_to_delete_projects": "Du bist kurz davor folgende Projekte zu löschen:", "about_to_delete_tag": "Du bist dabei, das folgende Stichwort zu löschen (darin enthaltene Projekte werden nicht gelöscht):", + "about_to_delete_template": "Du bist dabei, die folgende Vorlage zu löschen:", "about_to_delete_the_following_project": "Du bist dabei, das folgende Projekt zu löschen", "about_to_delete_the_following_projects": "Du bist dabei, die folgenden Projekte zu löschen", "about_to_delete_user_preamble": "Du bist dabei, __userName__ (__userEmail__) zu löschen. Das bedeutet:", @@ -161,6 +162,7 @@ "card_must_be_authenticated_by_3dsecure": "Deine Karte muss mit 3D Secure authentifiziert werden, bevor du fortfahren kannst", "card_payment": "Kartenzahlung", "careers": "Karriere", + "category": "Kategorie", "category_arrows": "Pfeile", "category_greek": "Griechisch", "category_misc": "Sonstiges", @@ -297,6 +299,7 @@ "delete_figure": "Abbildung löschen", "delete_projects": "Projekte archivieren", "delete_tag": "Stichwort löschen", + "delete_template": "Vorlage löschen", "delete_token": "Token löschen", "delete_user": "Nutzer löschen", "delete_your_account": "Lösche dein Konto", @@ -316,6 +319,7 @@ "do_not_have_acct_or_do_not_want_to_link": "Wenn du kein __appName__-Konto hast oder nicht mit deinem __institutionName__-Konto verknüpfen möchtest, klicke auf „__clickText__“.", "do_not_link_accounts": "Konten nicht verknüpfen", "do_you_want_to_change_your_primary_email_address_to": "Willst Du deine primäre E-Mail-Adresse in __email__ ändern?", + "do_you_want_to_overwrite_it": "Möchtest du es überschreiben?", "do_you_want_to_overwrite_them": "Willst Du sie überschreiben?", "documentation": "Dokumentation", "does_not_contain_or_significantly_match_your_email": "nicht mit Teilen deiner E-Mail-Adresse übereinstimmt", @@ -364,6 +368,7 @@ "edit_dictionary_remove": "Aus Wörterbuch entfernen", "edit_figure": "Abbildung bearbeiten", "edit_tag": "Schlagwort bearbeiten", + "edit_template": "Vorlage bearbeiten", "editing": "Bearbeitung", "editing_captions": "Beschriftungen bearbeiten", "editor_and_pdf": "Editor & PDF", @@ -408,6 +413,7 @@ "expiry": "Ablaufdatum", "export_csv": "CSV-Datei exportieren", "export_project_to_github": "Projekt nach GitHub exportieren", + "failed_to_publish_as_a_template": "Veröffentlichung als Vorlage fehlgeschlagen.", "failed_to_send_managed_user_invite_to_email": "Der Versand der Einladung für Verwaltete Benutzer an <0>__email__0> hat nicht funktioniert. Bitte versuche es später noch einmal.", "faq_how_does_free_trial_works_answer": "Während deines __len__-tägigen Probe-Abonnements erhältst du vollen Zugriff auf die Funktionen des von dir gewählten __appName__-Abonnements. Es besteht keine Verpflichtung, über die Testperiode hinaus fortzufahren. Deine Karte wird am Ende des __len__-tägigen Testzeitraums belastet, sofern du nicht vorher gekündigt hast. Um zu kündigen, gehe zu deinen Abonnementeinstellungen in deinem Konto.", "fast": "Schnell", @@ -856,6 +862,7 @@ "no_search_results": "Keine Suchergebnisse", "no_selection_select_file": "Derzeit ist keine Datei ausgewählt. Bitte wähle eine Datei aus dem Dateibaum aus.", "no_symbols_found": "Keine Symbole gefunden", + "no_templates_found": "Keine Vorlagen gefunden.", "no_thanks_cancel_now": "Nein, danke - Ich möchte nach wie vor jetzt stornieren", "no_update_email": "Nein, E-Mail-Adresse aktualisieren", "normal": "Normal", @@ -940,6 +947,7 @@ "please_change_primary_to_remove": "Bitte ändere deine primäre E-Mail-Adresse, um sie zu entfernen", "please_check_your_inbox_to_confirm": "Bitte überprüfe deinen E-Mail-Postfach, um deine Zugehörigkeit zu <0>__institutionName__0> zu bestätigen.", "please_compile_pdf_before_download": "Bitte kompiliere dein Projekt, bevor du das PDF herunterlädst", + "please_compile_pdf_before_publish_as_template": "Bitte kompiliere dein Projekt, bevor du es als Vorlage veröffentlichst.", "please_compile_pdf_before_word_count": "Bitte kompiliere dein Projekt, bevor du eine Wortzählung durchführst.", "please_confirm_email": "Bitte bestätige deine E-Mail-Adresse __emailAddress__, indem du auf den Link in der Bestätigungs-E-Mail klickst", "please_confirm_your_email_before_making_it_default": "Bitte bestätige deine E-Mail-Adresse, bevor du sie zur primären machst.", @@ -1198,6 +1206,7 @@ "template_not_found_description": "Diese Methode zum Erstellen von Projekten aus Vorlagen wurde entfernt. Besuche unsere Vorlagengalerie, um weitere Vorlagen zu finden.", "template_title_taken_from_project_title": "Der Vorlagentitel wird automatisch aus dem Projekttitel übernommen", "template_top_pick_by_overleaf": "Diese Vorlage wurde von Overleaf-Mitarbeitern aufgrund ihrer hohen Qualität ausgewählt", + "template_with_this_title_exists_and_owned_by_x": "Eine Vorlage mit diesem Namen existiert bereits, und der Besitzer ist: __x__.", "templates": "Vorlagen", "templates_admin_source_project": "Administration: Quellprojekt", "templates_page_title": "Vorlagen - Zeitschriften, Lebensläufe, Präsentationen, Berichte und mehr", @@ -1267,6 +1276,7 @@ "try_it_for_free": "Probiere es kostenlos aus", "try_now": "Jetzt versuchen", "try_premium_for_free": "Teste Premium kostenlos", + "try_recompile_project": "Versuche bitte, das Projekt von Grund auf neu zu kompilieren.", "try_recompile_project_or_troubleshoot": "Versuche bitte, das Projekt von Grund auf neu zu kompilieren. Wenn das Problem weiterhin besteht, findest Du im <0>Troubleshooting Guide0> weitere Hilfe", "try_to_compile_despite_errors": "Versuche, trotz Fehler zu kompilieren", "turn_off_link_sharing": "Deaktiviere die Linkfreigabe", @@ -1279,6 +1289,7 @@ "unconfirmed": "Unbestätigt", "unfold_line": "Zeile ausklappen", "university": "Universität", + "unknown": "Unbekannt", "unlimited": "Unbegrenzt", "unlimited_collabs": "Unbeschränkt viele Mitarbeiter", "unlimited_projects": "Unbegrenzte Projekte", @@ -1352,8 +1363,10 @@ "x_price_per_year": "__price__ pro Jahr", "year": "Jahr", "yes_that_is_correct": "Ja, das ist richtig", + "you": "Du", "you_can_now_log_in_sso": "Du kannst dich jetzt über deine Institution anmelden und möglicherweise <0>kostenlose __appName__ „Professionell“-Funktionen0> erhalten!", "you_can_opt_in_and_out_of_the_program_at_any_time_on_this_page": "Du kannst dich jederzeit auf dieser Seite für das Beta-Programm an- und abmelden", + "you_cant_overwrite_it": "Überschreiben ist nicht möglich.", "you_have_added_x_of_group_size_y": "Du hast <0>__addedUsersSize__0> von <1>__groupSize__1> verfügbaren Mitgliedern hinzugefügt", "you_will_be_able_to_contact_us_any_time_to_share_your_feedback": "Du kannst uns jederzeit kontaktieren, um uns dein Feedback mitzuteilen", "your_affiliation_is_confirmed": "Deine Zugehörigkeit zu <0>__institutionName__0> ist bestätigt.", diff --git a/services/web/locales/en.json b/services/web/locales/en.json index 910621f51a..5ea099a1bf 100644 --- a/services/web/locales/en.json +++ b/services/web/locales/en.json @@ -26,6 +26,7 @@ "about_to_delete_cert": "You are about to delete the following certificate:", "about_to_delete_projects": "You are about to delete the following projects:", "about_to_delete_tag": "You are about to delete the following tag (any projects in them will not be deleted):", + "about_to_delete_template": "You are about to delete the following template:", "about_to_delete_the_following_project": "You are about to delete the following project", "about_to_delete_the_following_projects": "You are about to delete the following projects", "about_to_delete_user_preamble": "You’re about to delete __userName__ (__userEmail__). Doing this will mean:", @@ -280,6 +281,7 @@ "card_payment": "Card payment", "careers": "Careers", "categories": "Categories", + "category": "Category", "category_arrows": "Arrows", "category_greek": "Greek", "category_misc": "Misc", @@ -499,6 +501,7 @@ "delete_sso_config": "Delete SSO configuration", "delete_table": "Delete table", "delete_tag": "Delete Tag", + "delete_template": "Delete template", "delete_token": "Delete token", "delete_user": "Delete user", "delete_your_account": "Delete your account", @@ -615,6 +618,7 @@ "edit_figure": "Edit figure", "edit_sso_configuration": "Edit SSO Configuration", "edit_tag": "Edit Tag", + "edit_template": "Edit template", "edit_your_custom_dictionary": "Edit your custom dictionary", "editing": "Editing", "editing_and_collaboration": "Editing and collaboration", @@ -711,6 +715,7 @@ "explore_all_plans": "Explore all plans", "export_csv": "Export CSV", "export_project_to_github": "Export Project to GitHub", + "failed_to_publish_as_a_template": "Failed to publish as a template.", "failed_to_send_group_invite_to_email": "Failed to send Group invite to <0>__email__0>. Please try again later.", "failed_to_send_managed_user_invite_to_email": "Failed to send Managed User invite to <0>__email__0>. Please try again later.", "failed_to_send_sso_link_invite_to_email": "Failed to send SSO invite reminder to <0>__email__0>. Please try again later.", @@ -1444,6 +1449,7 @@ "no_search_results": "No Search Results", "no_selection_select_file": "Currently, no file is selected. Please select a file from the file tree.", "no_symbols_found": "No symbols found", + "no_templates_found": "No templates found.", "no_thanks_cancel_now": "No thanks, I still want to cancel", "no_update_email": "No, update email", "non_deletable_entity": "The specified entity may not be deleted", @@ -1532,7 +1538,7 @@ "overleaf_labs": "Overleaf Labs", "overleaf_logo": "Overleaf logo", "overleaf_plans_and_pricing": "overleaf plans and pricing", - "overleaf_template_gallery": "overleaf template gallery", + "overleaf_template_gallery": "overleaf ce template gallery", "overleafs_functionality_meets_my_needs": "Overleaf’s functionality meets my needs.", "overview": "Overview", "overwrite": "Overwrite", @@ -1624,6 +1630,7 @@ "please_change_primary_to_remove": "Please change your primary email in order to remove", "please_check_your_inbox_to_confirm": "Please check your email inbox to confirm your <0>__institutionName__0> affiliation.", "please_compile_pdf_before_download": "Please compile your project before downloading the PDF", + "please_compile_pdf_before_publish_as_template": "Please compile your project before publishing it as a template", "please_compile_pdf_before_word_count": "Please compile your project before performing a word count", "please_confirm_email": "Please confirm your email __emailAddress__ by clicking on the link in the confirmation email ", "please_confirm_primary_email_or_edit": "Please confirm your primary email address __emailAddress__. To edit it, go to <0>Account settings0>.", @@ -1666,6 +1673,7 @@ "presentation": "Presentation", "presentation_mode": "Presentation mode", "press_and_awards": "Press & awards", + "prev": "Prev", "previous_page": "Previous page", "price": "Price", "primarily_work_study_question": "Where do you primarily work or study?", @@ -1728,7 +1736,7 @@ "pt": "Portuguese", "public": "Public", "publish": "Publish", - "publish_as_template": "Manage Template", + "publish_as_template": "Publish as a Template", "publisher_account": "Publisher Account", "publishing": "Publishing", "pull_github_changes_into_sharelatex": "Pull GitHub changes into __appName__", @@ -2215,6 +2223,7 @@ "template_not_found_description": "This way of creating projects from templates has been removed. Please visit our template gallery to find more templates.", "template_title_taken_from_project_title": "The template title will be taken automatically from the project title", "template_top_pick_by_overleaf": "This template was hand-picked by Overleaf staff for its high quality", + "template_with_this_title_exists_and_owned_by_x": "A template with this title already exists and is owned by __x__.", "templates": "Templates", "templates_admin_source_project": "Admin: Source Project", "templates_lowercase": "templates", @@ -2418,6 +2427,7 @@ "try_now": "Try Now", "try_papers_for_free": "Try Papers for free", "try_premium_for_free": "Try Premium for free", + "try_recompile_project": "Please try recompiling the project from scratch.", "try_recompile_project_or_troubleshoot": "Please try recompiling the project from scratch, and if that doesn’t help, follow our <0>troubleshooting guide0>.", "try_relinking_provider": "It looks like you need to re-link your __provider__ account.", "try_the_new_editor": "Try the new editor", @@ -2662,6 +2672,7 @@ "you_can_still_use_your_premium_features": "You can still use your premium features until the pause becomes active.", "you_cant_add_or_change_password_due_to_sso": "You can’t add or change your password because your group or organization uses <0>single sign-on (SSO)0>.", "you_cant_join_this_group_subscription": "You can’t join this group subscription", + "you_cant_overwrite_it": "You can’t overwrite it.", "you_cant_reset_password_due_to_sso": "You can’t reset your password because your group or organization uses SSO. <0>Log in with SSO0>.", "you_dont_have_any_add_ons_on_your_account": "You don’t have any add-ons on your account.", "you_dont_have_any_repositories": "You don’t have any repositories", diff --git a/services/web/locales/ru.json b/services/web/locales/ru.json index 13975aadf2..62d9adaa6c 100644 --- a/services/web/locales/ru.json +++ b/services/web/locales/ru.json @@ -20,6 +20,7 @@ "about_to_archive_projects": "Вы собираетесь архивировать следующие проекты:", "about_to_delete_cert": "Вы собираетесь удалить следующий сертификат:", "about_to_delete_projects": "Следующие проекты будут удалены:", + "about_to_delete_template": "Следующий шаблон будет удален:", "about_to_delete_the_following_project": "Вы собираетесь удалить следующий проект:", "about_to_delete_the_following_projects": "Вы собираетесь удалить следующие проекты:", "about_to_leave_project": "Вы собираетесь покинуть этот проект:", @@ -69,6 +70,7 @@ "cancel_your_subscription": "Остановить подписку", "cant_find_email": "Извините, данный адрес не зарегистрирован.", "cant_find_page": "К сожалению, страница не найдена", + "category": "Категория", "change": "Изменить", "change_password": "Изменение пароля", "change_plan": "Сменить тарифный план", @@ -127,9 +129,11 @@ "delete_account_warning_message_3": "Вы собираетесь удалить все данные Вашего аккаунта, включая все Ваши проекты и настройки. Пожалуйста, введите адрес электронной почты и пароль Вашего аккаунта в форму внизу для продолжения.", "delete_and_leave_projects": "Удалить или оставить проекты", "delete_projects": "Удалить проекты", + "delete_template": "Удалить шаблон", "delete_your_account": "Удалить аккаунт", "deleting": "Удаление", "disconnected": "Разъединен", + "do_you_want_to_overwrite_it": "Перезаписать?", "documentation": "Документация", "doesnt_match": "Не совпадает", "done": "Готово", @@ -138,6 +142,7 @@ "download_zip_file": "Скачать архив (.zip)", "dropbox_sync": "Синхронизация с Dropbox", "dropbox_sync_description": "Синхронизируйте Ваши __appName__ проекты с Вашим Dropbox. Изменения в __appName__ автоматически сохраняются в Вашем Dropbox, и наоборот.", + "edit_template": "Редактировать шаблон", "editing": "Редактор", "email": "Адрес электронной почты", "email_already_registered": "Этот адрес уже зарегистрирован.", @@ -149,6 +154,7 @@ "example_project": "Использовать пример", "expiry": "Срок действия", "export_project_to_github": "Экспорт проекта на GitHub", + "failed_to_publish_as_a_template": "Не удалось создать шаблон.", "fast": "быстрый", "features": "Возможности", "february": "Февраль", @@ -272,6 +278,7 @@ "no_projects": "Нет проектов", "no_search_results": "Ничего не найдено", "no_thanks_cancel_now": "Нет, спасибо - я хочу удалить сейчас", + "no_templates_found": "Шаблоны не найдены.", "normal": "нормальный", "not_now": "Не сейчас", "notification_project_invite": "__userName__ приглашает Вас принять участие в проекте __projectName__,Присоединиться", @@ -300,7 +307,8 @@ "plans_amper_pricing": "Тарифы", "plans_and_pricing": "Тарифные планы", "please_compile_pdf_before_download": "Пожалуйста, скомпилируйте проект перед загрузкой PDF", - "please_compile_pdf_before_word_count": "Пожалуйста, скомпилируйте проект, прежде чем подсчитывать количество слов!", + "please_compile_pdf_before_publish_as_template": "Пожалуйста, скомпилируйте проект, прежде чем создавать из него шаблон", + "please_compile_pdf_before_word_count": "Пожалуйста, скомпилируйте проект, прежде чем подсчитывать количество слов.", "please_enter_email": "Пожалуйста, введите адрес электронной почты", "please_refresh": "Пожалуйста, обновите страницу для продолжения", "please_set_a_password": "Пожалуйста, укажите пароль", @@ -409,6 +417,7 @@ "syntax_validation": "Проверка кода", "take_me_home": "Вернуться в начало", "template_description": "Описание шаблона", + "template_with_this_title_exists_and_owned_by_x": "Уже есть шаблон с таким названием, его владелец __x__.", "templates": "Шаблоны", "terminated": "Компиляция отменена", "terms": "Условия", @@ -433,8 +442,10 @@ "total_words": "Количество слов", "tr": "Турецкий", "try_now": "Попробуйте", + "try_recompile_project": "Попробуйте скомпилировать проект заново.", "uk": "Украинский", "university": "Университет", + "unknown": "Неизвестный", "unlimited_collabs": "Неограниченно число соавторов", "unlimited_projects": "Неограниченное число проектов", "unlink": "Отсоединить", @@ -462,6 +473,8 @@ "welcome_to_sl": "Добро пожаловать в __appName__", "word_count": "Количество слов", "year": "год", + "you": "Вы", + "you_cant_overwrite_it": "Перезаписать нельзя.", "you_have_added_x_of_group_size_y": "Вы добавили <0>__addedUsersSize__0> из <1>__groupSize__1> доступных участников", "your_plan": "Ваш тариф", "your_projects": "Созданные мной", diff --git a/services/web/modules/template-gallery/app/src/CleanHtml.mjs b/services/web/modules/template-gallery/app/src/CleanHtml.mjs new file mode 100644 index 0000000000..1e38437016 --- /dev/null +++ b/services/web/modules/template-gallery/app/src/CleanHtml.mjs @@ -0,0 +1,22 @@ +import sanitizeHtml from 'sanitize-html' + +const sanitizeOptions = { + linksOnly: { + allowedTags: ["a"], + allowedAttributes: { "a": ["href"] } + }, + reachText: { + allowedTags: ["h1", "h2", "h3", "h4", "h5", "h6", + "p", "blockquote", "ul", "ol", "li", + "em", "strong", "s", "code", "pre", "hr", "a"], + allowedAttributes: { "a": ["href"] } + }, + plainText: { + allowedTags: [], + allowedAttributes: {}, + }, +} + +export function cleanHtml(text, sanitizeType) { + return sanitizeHtml(text, sanitizeOptions[sanitizeType]) +} diff --git a/services/web/modules/template-gallery/app/src/TemplateErrors.mjs b/services/web/modules/template-gallery/app/src/TemplateErrors.mjs new file mode 100644 index 0000000000..6b7ed014fe --- /dev/null +++ b/services/web/modules/template-gallery/app/src/TemplateErrors.mjs @@ -0,0 +1,13 @@ +import OError from '@overleaf/o-error' + +export class TemplateNameConflictError extends OError { + constructor(ownerId, message = 'template_with_this_title_exists_and_owned_by_x') { + super(message, { ownerId }) + } +} + +export class RecompileRequiredError extends OError { + constructor(message = 'Recompile required') { + super(message, { status: 400 }) + } +} diff --git a/services/web/modules/template-gallery/app/src/TemplateGalleryController.mjs b/services/web/modules/template-gallery/app/src/TemplateGalleryController.mjs new file mode 100644 index 0000000000..1d88f50d1a --- /dev/null +++ b/services/web/modules/template-gallery/app/src/TemplateGalleryController.mjs @@ -0,0 +1,162 @@ +import logger from '@overleaf/logger' +import ErrorController from '../../../../app/src/Features/Errors/ErrorController.js' +import Errors from '../../../../app/src/Features/Errors/Errors.js' +import SessionManager from '../../../../app/src/Features/Authentication/SessionManager.js' +import TemplateGalleryManager from'./TemplateGalleryManager.mjs' +import { getUserName } from './TemplateGalleryHelper.mjs' +import { TemplateNameConflictError, RecompileRequiredError } from './TemplateErrors.mjs' +import Settings from '@overleaf/settings' + +async function createTemplateFromProject(req, res, next) { + const t = req.i18n.translate + try { + const userId = SessionManager.getLoggedInUserId(req.session) + const result = await TemplateGalleryManager.createTemplateFromProject({ + projectId: req.params.Project_id, + userId, + templateSource: req.body, + }) + if (result.conflict) { + const ownerName = (result.templateOwnerName === 'you') ? t('you') : result.templateOwnerName + const message = `${t('template_with_this_title_exists_and_owned_by_x', { x: ownerName })} ` + + t(result.canOverride ? 'do_you_want_to_overwrite_it' : 'you_cant_overwrite_it') + return res.status(409).json({ canOverride: result.canOverride, message }) + } + return res.status(200).json({ template_id: result.templateId }) + } catch (error) { + if (error instanceof Errors.InvalidNameError) { + return res.status(error.info?.status || 400).json({ message: error.message }) + } + + const mainMessage = t('failed_to_publish_as_a_template') + if (error instanceof RecompileRequiredError) { + return res.status(error.info?.status || 400).json({ + message: `${mainMessage} ${t('try_recompile_project')}` + }) + } + return res.status(400).json({ message: mainMessage }) + } +} + +async function editTemplate(req, res, next) { + const t = req.i18n.translate + try { + const result = await TemplateGalleryManager.editTemplate({ + templateId: req.params.template_id, + updates: req.body + }) + res.status(200).json(result) + } catch (error) { + if (error instanceof TemplateNameConflictError) { + const ownerId = error.info?.ownerId + const userId = SessionManager.getLoggedInUserId(req.session) + const ownerName = (ownerId === userId) + ? t('you') + : await getUserName(ownerId) || t('unknown') + const message = t(error.message, { x: ownerName }) + return res.status(409).json({ message }) + } + if (error instanceof Errors.InvalidNameError) { + return res.status(error.info?.status || 400).json({ message: error.message }) + } + logger.error({ error }, 'Failure saving template') + return res.status(500).json({ message: t('something_went_wrong_server') }) + } +} + +async function deleteTemplate(req, res, next) { + const t = req.i18n.translate + try { + await TemplateGalleryManager.deleteTemplate({ + templateId: req.params.template_id, + version: req.body.version + }) + res.sendStatus(200) + } catch (error) { + logger.error({ error }, 'Failure deleting template') + return res.status(500).json({ message: t('something_went_wrong_server') }) + } +} + +async function getTemplatePreview(req, res, next) { + try { + const templateId = req.params.template_id + const { version, style } = req.query + + const { stream, contentType } = await TemplateGalleryManager.fetchTemplatePreview({ templateId, version, style }) + + res.setHeader('Content-Type', contentType) + stream.pipe(res) + } catch (error) { + if (error.info?.status == 404) { + return ErrorController.notFound(req, res, next) + } + return res.status(error.info?.status || 400).json(error.info) + } +} + +async function templatesCategoryPage(req, res, next) { + const t = req.i18n.translate + try { + let { category } = req.params + const result = await TemplateGalleryManager.getTemplatesPageData(category) + + let title + if (result.categoryName) { + title = t('latex_templates') + ' — ' + result.categoryName + } else { + category = null + title = t('templates_page_title') + } + res.render('template_gallery/template-gallery', { + title, + category, + }) + } catch (error) { + next(error) + } +} + +async function templateDetailsPage(req, res, next) { + const t = req.i18n.translate + try { + const template = await TemplateGalleryManager.getTemplate('_id', req.params.template_id) + res.render('template_gallery/template', { + title: `${t('template')}: ${template.name}`, + template: JSON.stringify(template), + languages: Settings.languages, + }) + } catch (error) { + return ErrorController.notFound(req, res, next) + } +} + +async function getTemplateJSON(req, res, next) { + try { + const { key, val } = req.query + const template = await TemplateGalleryManager.getTemplate(key, val) + res.json(template) + } catch (error) { + next(error) + } +} + +async function getCategoryTemplatesJSON(req, res, next) { + try { + const result = await TemplateGalleryManager.getCategoryTemplates(req.query) + res.json(result) + } catch (error) { + next(error) + } +} + +export default { + createTemplateFromProject, + editTemplate, + deleteTemplate, + getTemplatePreview, + templatesCategoryPage, + templateDetailsPage, + getTemplateJSON, + getCategoryTemplatesJSON, +} diff --git a/services/web/modules/template-gallery/app/src/TemplateGalleryHelper.mjs b/services/web/modules/template-gallery/app/src/TemplateGalleryHelper.mjs new file mode 100644 index 0000000000..97fc86d650 --- /dev/null +++ b/services/web/modules/template-gallery/app/src/TemplateGalleryHelper.mjs @@ -0,0 +1,238 @@ +import { marked } from 'marked' +import request from 'request' +import logger from '@overleaf/logger' +import settings from '@overleaf/settings' +import Errors from '../../../../app/src/Features/Errors/Errors.js' +import ProjectGetter from '../../../../app/src/Features/Project/ProjectGetter.js' +import ProjectLocator from '../../../../app/src/Features/Project/ProjectLocator.js' +import ProjectZipStreamManager from '../../../../app/src/Features/Downloads/ProjectZipStreamManager.mjs' +import DocumentUpdaterHandler from '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js' +import ClsiManager from '../../../../app/src/Features/Compile/ClsiManager.js' +import CompileManager from '../../../../app/src/Features/Compile/CompileManager.js' +import UserGetter from '../../../../app/src/Features/User/UserGetter.js' +import { fetchStreamWithResponse } from '@overleaf/fetch-utils' +import { Template } from './models/Template.js' +import { RecompileRequiredError } from './TemplateErrors.mjs' +import { cleanHtml } from './CleanHtml.mjs' + +const TIMEOUT = 30000 + +const MAX_PROJECT_NAME_LENGTH = 150 +const MAX_FORM_INPUT_LENGTH = 512 +const MAX_TEMPLATE_DESCRIPTION_LENGTH = 4096 + +// Configure marked for CommonMark-only parsing +marked.setOptions({ + gfm: false, + breaks: false, + headerIds: false, + mangle: false, +}) + +marked.use({ + renderer: { + html: () => '' // strips any HTML tags + } +}) + +function _createZipStreamForProjectAsync(projectId) { + return new Promise((resolve, reject) => { + ProjectZipStreamManager.createZipStreamForProject(projectId, (err, archive) => { + if (err) { + return reject(err) + } + archive.on('error', (err) => reject(err)) + resolve(archive) + }) + }) +} + +export function validateTemplateInput({ name, descriptionMD, authorMD, license }) { + if (name?.length > MAX_PROJECT_NAME_LENGTH) { + throw new Errors.InvalidNameError(`Template title exceeds the maximum length of ${MAX_PROJECT_NAME_LENGTH} characters.`) + } + if (descriptionMD?.length > MAX_TEMPLATE_DESCRIPTION_LENGTH) { + throw new Errors.InvalidNameError(`Template description exceeds the maximum length of ${MAX_TEMPLATE_DESCRIPTION_LENGTH} characters.`) + } + if (authorMD?.length > MAX_FORM_INPUT_LENGTH) { + throw new Errors.InvalidNameError('Author name is too long.') + } + if (license?.length > MAX_FORM_INPUT_LENGTH) { + throw new Errors.InvalidNameError('License name is too long.') + } +} + +export async function canUserOverrideTemplate(template, userId) { + let templateOwnerId = template.owner + let templateOwnerName = 'you' + let userIsOwner = true + let userIsAdmin + if (templateOwnerId != userId) { + userIsOwner = false + try { + userIsAdmin = (await UserGetter.promises.getUser(userId, { isAdmin: 1 })).isAdmin + } catch { + logger.error({ error, userId }, 'Logged in user does not exist, strange...') + userIsAdmin = false + } + templateOwnerName = await getUserName(templateOwnerId) || 'unknown' + } + const canOverride = userIsOwner || userIsAdmin + return { canOverride, templateOwnerName } +} + +export async function getUserName(userId) { + try { + const user = await UserGetter.promises.getUser(userId, { + first_name: 1, + last_name: 1, + }) + return ((user?.first_name || "") + " " + (user?.last_name || "")).trim() + } catch (error) { + return 'unknown' + } +} + +export async function generateTemplateData(projectId, { + descriptionMD, + authorMD, + category, + license, + name, +}) { + try { + await DocumentUpdaterHandler.promises.flushProjectToMongo(projectId) + + const project = await ProjectGetter.promises.getProject(projectId, { + imageName: true, + compiler: true, + spellCheckLanguage: true, + rootDoc_id: true, + rootFolder: true, + }) + + const { path } = await ProjectLocator.promises.findRootDoc({ project }) + const mainFile = path.fileSystem.replace(/^\/+/, '') + const template = { + name, + category, + authorMD, + descriptionMD, + license, + mainFile, + compiler: project.compiler, + imageName: project.imageName, + language: project.spellCheckLanguage, + lastUpdated: new Date(), + } + + await renderTemplateHtmlFields(template) + return template + } catch (error) { + logger.error({ error, projectId }, 'Failed to retrieve project data') + throw error + } +} + +export async function uploadTemplateAssets(projectId, userId, build, template) { + let zipStream, pdfStream + try { + [zipStream, pdfStream] = await Promise.all([ + _createZipStreamForProjectAsync(projectId), + CompileManager.promises + .getProjectCompileLimits(projectId) + .then((limits) => + ClsiManager.promises.getOutputFileStream(projectId, userId, limits, undefined, build, 'output.pdf') + ) + ]) + } catch (error) { + logger.error({ error, projectId }, 'No output.pdf?') + throw new RecompileRequiredError() + } + try { + const templateUrl = `${settings.apis.filestore.url}/template/${template._id}/v/${template.version}` + const zipUrl = `${templateUrl}/zip` + const pdfUrl = `${templateUrl}/pdf` + const [zipReq, pdfReq] = await Promise.all([ + fetchStreamWithResponse(zipUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/octet-stream', + }, + body: zipStream, + signal: AbortSignal.timeout(TIMEOUT), + }), + fetchStreamWithResponse(pdfUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/pdf', + }, + body: pdfStream, + signal: AbortSignal.timeout(TIMEOUT), + }), + ]) + const uploadErrors = [] + if (zipReq?.response.status !== 200) { + uploadErrors.push({ file: 'zip', uri: zipUrl, statusCode: zipReq.response.status }) + } + if (pdfReq?.response.status !== 200) { + uploadErrors.push({ file: 'pdf', uri: pdfUrl, statusCode: pdfReq.response.status }) + } + if (uploadErrors.length > 0) { + logger.error({ uploadErrors }, 'Template upload failed') + throw new RecompileRequiredError() + } + } catch (error) { + if (error instanceof RecompileRequiredError) throw error + throw error + } +} + +export async function deleteTemplateAssets(templateId, version, deleteFromDb) { + if (deleteFromDb) { + try { + await Template.deleteOne({ _id: templateId }).exec() + } catch (error) { + logger.error({ err, templateId }, 'Failed to delete template from MongoDB') + throw error + } + } + + // kick off file deletions, but don't wait + const baseUrl = settings.apis.filestore.url + const urlTemplate = `${baseUrl}/template/${templateId}/v/${version}/zip` + const urlImages = `${baseUrl}/template/${templateId}/v/${version}/pdf` + + const optsTemplate = { + method: 'DELETE', + uri: urlTemplate, + timeout: TIMEOUT, + } + + const optsImages = { + method: 'DELETE', + uri: urlImages, + timeout: TIMEOUT, + } + + request(optsTemplate, (err, response) => { + if (err) + logger.warn({ err, templateId }, 'Failed to delete template zip from filestore') + }) + + request(optsImages, (err, response) => { + if (err) + logger.warn({ err, templateId }, 'Failed to delete images from filestore') + }) +} + +export function renderTemplateHtmlFields(updates) { + if (updates.descriptionMD !== undefined) { + const descriptionRawHTML = marked.parse(updates.descriptionMD) + updates.description = cleanHtml(descriptionRawHTML, "reachText") + } + if (updates.authorMD !== undefined) { + const authorRawHTML = marked.parse(updates.authorMD) + updates.author = cleanHtml(authorRawHTML, "linksOnly") + } +} diff --git a/services/web/modules/template-gallery/app/src/TemplateGalleryManager.mjs b/services/web/modules/template-gallery/app/src/TemplateGalleryManager.mjs new file mode 100644 index 0000000000..f3993d6854 --- /dev/null +++ b/services/web/modules/template-gallery/app/src/TemplateGalleryManager.mjs @@ -0,0 +1,214 @@ +import _ from 'lodash' +import logger from '@overleaf/logger' +import { Readable } from 'stream' +import settings from '@overleaf/settings' +import { OError } from '../../../../app/src/Features/Errors/Errors.js' +import { Template } from './models/Template.js' +import { + validateTemplateInput, + renderTemplateHtmlFields, + uploadTemplateAssets, + deleteTemplateAssets, + canUserOverrideTemplate, + generateTemplateData +} from './TemplateGalleryHelper.mjs' +import { cleanHtml } from './CleanHtml.mjs' +import { TemplateNameConflictError } from './TemplateErrors.mjs' +import { fetchStreamWithResponse } from '@overleaf/fetch-utils' +const TIMEOUT = 30000 + +async function editTemplate({ templateId, updates }) { + + validateTemplateInput(updates) + + const template = await Template.findById(templateId) + if (!template) { + throw new OError('Current template not found, strange...', { status: 500, templateId }) + } + + if (updates.name) { + const conflictingTemplate = await Template.findOne( + { name: updates.name, _id: { $ne: templateId } }, + { owner: true } + ).exec() + if (conflictingTemplate) { + throw new TemplateNameConflictError(String(conflictingTemplate.owner)) + } + } + + await renderTemplateHtmlFields(updates) + updates.lastUpdated = new Date() + Object.assign(template, updates) + + await template.save() + + return updates +} + +async function deleteTemplate({ templateId, version }) { + await deleteTemplateAssets(templateId, version, true) +} + +async function createTemplateFromProject({ projectId, userId, templateSource }) { + validateTemplateInput(templateSource) + let template = await Template.findOne({ name: templateSource.name }).exec() + + if (template && !templateSource.override) { + const { canOverride, templateOwnerName } = await canUserOverrideTemplate(template, userId) + + return { + conflict: true, + canOverride, + templateOwnerName + } + } + + const templateData = await generateTemplateData(projectId, templateSource) + + let previousVersionExists + if (!template) { + template = new Template(templateData) + template.owner = userId + previousVersionExists = false + } else { + Object.assign(template, templateData, { + version: template.version + 1, + }) + previousVersionExists = true + } + + await uploadTemplateAssets(projectId, userId, templateSource.build, template) + await template.save() + + if (previousVersionExists) { + deleteTemplateAssets(template._id, template.version - 1, false) + } + return { + conflict: false, + templateId: template._id, + } +} + +async function fetchTemplatePreview({ templateId, version, style }) { + if (!templateId || !version) { + throw new OError('Template ID and version are required', { status: 404 }) + } + const styleParam = style ? `style=${style}` : '' + const isImage = (style === 'preview' || style === 'thumbnail') + + if (style && !isImage) { + throw new OError('Wrong style', { status: 404, style }) + } + + const pdfUrl = `${settings.apis.filestore.url}/template/${templateId}/v/${version}/pdf?${styleParam}` + const { response } = await fetchStreamWithResponse(pdfUrl, { + method: 'GET', + signal: AbortSignal.timeout(TIMEOUT), + }) + + if (!response.ok) { + throw new OError(`Failed to fetch file: ${response.statusText}`, { status: 400, templateId, version, styleParam }) + } + + return { + stream: Readable.from(response.body), + contentType: isImage ? 'application/octet-stream' : 'application/pdf' + } +} + +async function getTemplatesPageData(category) { + const categoryName = settings.templateLinks.find(item => item.url.endsWith(`/${category}`))?.name + const templateLinks = categoryName ? undefined : settings.templateLinks.filter(link => link.url !== '/templates/all') + return { + categoryName, + templateLinks + } +} + +async function getTemplate(key, val) { + if (!key || !val) { + logger.warn('No key or val provided to getTemplate') + return null + } + + const query = { [key]: val } + const template = await Template.findOne(query).exec() + if (!template) return null + + return _formatTemplateForPage(template) +} + +async function getCategoryTemplates(reqQuery) { + const { + category, + by = 'lastUpdated', + order = 'desc', + } = reqQuery || {} + + const query = (category === 'all') ? {} : { category : '/templates/' + category } + const projection = { _id : 1, version : 1, name : 1, author : 1, description : 1, lastUpdated : 1 } + const allTemplates = await Template.find(query, projection).exec() + const formattedTemplates = allTemplates.map(_formatTemplateForList) + const sortedTemplates = _sortTemplates(formattedTemplates, { by, order }) + + return { + totalSize: sortedTemplates.length, + templates: sortedTemplates, + } +} + +function _sortTemplates(templates, sort) { + if ( + (sort.by && !['lastUpdated', 'name'].includes(sort.by)) || + (sort.order && !['asc', 'desc'].includes(sort.order)) + ) { + throw new OError('Invalid sorting criteria', { status: 400, sort }) + } + const sortedTemplates = _.orderBy( + templates, + [sort.by || 'lastUpdated'], + [sort.order || 'desc'] + ) + return sortedTemplates +} + +function _formatTemplateForList(template) { + return { + id: String(template._id), + version: String(template.version), + name: template.name, + author: cleanHtml(template.author, "plainText"), + description: cleanHtml(template.description, "plainText"), + lastUpdated: template.lastUpdated, + } +} + +function _formatTemplateForPage(template) { + return { + id: template._id.toString(), + version: template.version.toString(), + category: template.category, + name: template.name, + author: cleanHtml(template.author, "linksOnly"), + authorMD: template.authorMD, + description: cleanHtml(template.description, "reachText"), + descriptionMD: template.descriptionMD, + license: template.license, + lastUpdated: template.lastUpdated, + owner: template.owner, + mainFile: template.mainFile, + compiler: template.compiler, + imageName: template.imageName, + language: template.language, + } +} + +export default { + createTemplateFromProject, + editTemplate, + deleteTemplate, + getTemplate, + getCategoryTemplates, + fetchTemplatePreview, + getTemplatesPageData, +} diff --git a/services/web/modules/template-gallery/app/src/TemplateGalleryRouter.mjs b/services/web/modules/template-gallery/app/src/TemplateGalleryRouter.mjs new file mode 100644 index 0000000000..b76f86d718 --- /dev/null +++ b/services/web/modules/template-gallery/app/src/TemplateGalleryRouter.mjs @@ -0,0 +1,82 @@ +import logger from '@overleaf/logger' + +import AuthenticationController from '../../../../app/src/Features/Authentication/AuthenticationController.js' +import RateLimiterMiddleware from '../../../../app/src/Features/Security/RateLimiterMiddleware.js' +import { RateLimiter } from '../../../../app/src/infrastructure/RateLimiter.js' +import TemplateGalleryController from './TemplateGalleryController.mjs' + +const rateLimiterNewTemplate = new RateLimiter('create-template-from-project', { + points: 20, + duration: 60, +}) +const rateLimiter = new RateLimiter('template-gallery', { + points: 60, + duration: 60, +}) +const rateLimiterThumbnails = new RateLimiter('template-gallery-thumbnails', { + points: 240, + duration: 60, +}) + + +export default { + rateLimiter, + apply(webRouter) { + logger.debug({}, 'Init templates router') + + webRouter.post( + '/template/new/:Project_id', + AuthenticationController.requireLogin(), + RateLimiterMiddleware.rateLimit(rateLimiterNewTemplate), + TemplateGalleryController.createTemplateFromProject + ) + + webRouter.get( + '/template/:template_id', + RateLimiterMiddleware.rateLimit(rateLimiter), + TemplateGalleryController.templateDetailsPage + ) + + webRouter.post( + '/template/:template_id/edit', + AuthenticationController.requireLogin(), + RateLimiterMiddleware.rateLimit(rateLimiter), + TemplateGalleryController.editTemplate + ) + + webRouter.delete( + '/template/:template_id/delete', + AuthenticationController.requireLogin(), + RateLimiterMiddleware.rateLimit(rateLimiter), + TemplateGalleryController.deleteTemplate + ) + + webRouter.get( + '/templates/:category?', + RateLimiterMiddleware.rateLimit(rateLimiter), + TemplateGalleryController.templatesCategoryPage + ) + + webRouter.get( + '/api/template', + RateLimiterMiddleware.rateLimit(rateLimiter), + TemplateGalleryController.getTemplateJSON + ) + + webRouter.get( + '/api/templates', + RateLimiterMiddleware.rateLimit(rateLimiter), + TemplateGalleryController.getCategoryTemplatesJSON + ) + + webRouter.get( + '/template/:template_id/preview', + (req, res, next) => { + req.query.style === 'thumbnail' + ? RateLimiterMiddleware.rateLimit(rateLimiterThumbnails)(req, res, next) + : RateLimiterMiddleware.rateLimit(rateLimiter)(req, res, next) + }, + TemplateGalleryController.getTemplatePreview + ) + }, +} diff --git a/services/web/modules/template-gallery/app/src/models/Template.js b/services/web/modules/template-gallery/app/src/models/Template.js new file mode 100644 index 0000000000..79792f150e --- /dev/null +++ b/services/web/modules/template-gallery/app/src/models/Template.js @@ -0,0 +1,33 @@ +const mongoose = require('../../../../../app/src/infrastructure/Mongoose') + +const { Schema } = mongoose +const { ObjectId } = Schema + +const TemplateSchema = new Schema( + { + name: { type: String, required: true }, + category: { type: String, required: true }, + description: { type: String }, + descriptionMD: { type: String }, + author: { type: String }, + authorMD: { type: String }, + license: { type: String, required: true }, + mainFile: { type: String, required: true }, + compiler: { type: String, required: true }, + imageName: { type: String }, + language: { type: String }, + version: { type: Number, default: 1, required: true }, + owner: { type: ObjectId, ref: 'User' }, + lastUpdated: { + type: Date, + default() { + return new Date() + }, + required: true + }, + }, + { minimize: false } +) + +exports.Template = mongoose.model('Template', TemplateSchema) +exports.TemplateSchema = TemplateSchema diff --git a/services/web/modules/template-gallery/index.mjs b/services/web/modules/template-gallery/index.mjs new file mode 100644 index 0000000000..16e39f46e7 --- /dev/null +++ b/services/web/modules/template-gallery/index.mjs @@ -0,0 +1,42 @@ +import Settings from '@overleaf/settings' +import TemplateGalleryRouter from './app/src/TemplateGalleryRouter.mjs' + +function boolFromEnv(env) { + if (env === undefined || env === null) return undefined + if (typeof env === "string") { + const envLower = env.toLowerCase() + if (envLower === 'true') return true + if (envLower === 'false') return false + } + throw new Error("Invalid value for boolean environment variable") +} + +let TemplateGalleryModule = {} + +if (process.env.OVERLEAF_TEMPLATE_GALLERY === 'true') { + TemplateGalleryModule = { + router: TemplateGalleryRouter, + } + + Settings.templates = { + nonAdminCanManage: boolFromEnv(process.env.OVERLEAF_NON_ADMIN_CAN_PUBLISH_TEMPLATES) + } + + const templateKeys = process.env.OVERLEAF_TEMPLATE_CATEGORIES + ? process.env.OVERLEAF_TEMPLATE_CATEGORIES + ' all' + : 'all' + + Settings.templateLinks = templateKeys.split(/\s+/).map(key => { + const envKeyBase = key.toUpperCase().replace(/-/g, "_") + const name = process.env[`TEMPLATE_${envKeyBase}_NAME`] || ( key === 'all' ? 'All templates' : key) + const description = process.env[`TEMPLATE_${envKeyBase}_DESCRIPTION`] || '' + + return { + name, + url: `/templates/${key}`, + description + } + }) +} + +export default TemplateGalleryModule diff --git a/services/web/types/template.ts b/services/web/types/template.ts new file mode 100644 index 0000000000..214c388969 --- /dev/null +++ b/services/web/types/template.ts @@ -0,0 +1,16 @@ +export type Template = { + id: string + version: number + name: string + lastUpdated: Date + author: string + authorMD: string + description: string + descriptionMD: string + license: string + category: string + compiler?: string + language?: string + owner: string +} +
{t('latex_templates_for_journal_articles')} +
{description}
{link.description}
{t('no_templates_found')}
{t('about_to_delete_template')}