--- date: "2023-05-25T16:00:00+02:00" slug: "guidelines-frontend" sidebar_position: 20 aliases: - /zh-tw/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. 使用語義化元素,優先使用`