--- date: "2023-05-25T16:00:00+02:00" title: "前端开发指南" slug: "guidelines-frontend" sidebar_position: 20 toc: false draft: false aliases: - /zh-cn/guidelines-frontend menu: sidebar: parent: "contributing" name: "前端开发指南" sidebar_position: 20 identifier: "guidelines-frontend" --- # 前端开发指南 ## 背景 Gitea 在其前端中使用[Fomantic-UI](https://fomantic-ui.com/introduction/getting-started.html)(基于[jQuery](https://api.jquery.com))和 [Vue3](https://vuejs.org/)。 HTML 页面由[Go HTML Template](https://pkg.go.dev/html/template)渲染。 源文件可以在以下目录中找到: * **CSS 样式**: `web_src/css/` * **JavaScript 文件**: `web_src/js/` * **Vue 组件**: `web_src/js/components/` * **Go HTML 模板**: `templates/` ## 通用准则 我们推荐使用[Google HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssguide.html)和[Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)。 ## Gitea 特定准则 1. 每个功能(Fomantic-UI/jQuery 模块)应放在单独的文件/目录中。 2. HTML 的 id 和 class 应使用 kebab-case,最好包含2-3个与功能相关的关键词。 3. 在 JavaScript 中使用的 HTML 的 id 和 class 应在整个项目中是唯一的,并且应包含2-3个与功能相关的关键词。建议在仅在 JavaScript 中使用的 class 中使用 `js-` 前缀。 4. 不应覆盖框架提供的 class 的 CSS 样式。始终使用具有2-3个与功能相关的关键词的新 class 名称来覆盖框架样式。Gitea 中的帮助 CSS 类在 `helpers.less` 中。 5. 后端可以通过使用`ctx.PageData["myModuleData"] = map[]{}`将复杂数据传递给前端,但不要将整个模型暴露给前端,以避免泄露敏感数据。 6. 简单页面和与 SEO 相关的页面使用 Go HTML 模板渲染生成静态的 Fomantic-UI HTML 输出。复杂页面可以使用 Vue3。 7. 明确变量类型,优先使用`elem.disabled = true`而不是`elem.setAttribute('disabled', 'anything')`,优先使用`$el.prop('checked', var === 'yes')`而不是`$el.prop('checked', var)`。 8. 使用语义化元素,优先使用`