前端开发
这个页面描述如何改变Flarum的用户界面—— 添加按钮、文字滚动、和闪耀的文本 🤩 添加按钮、文字滚动、和闪耀的文本 🤩
Remember, Flarum's frontend is a single-page JavaScript application. 我们不会用到Twig、Blade或任何其他的PHP模板。后端中存在的少数模板仅用于渲染针对搜索引擎优化的内容。所有对UI的改动都需要通过JavaScript实现。
Flarum有两个分开的前端应用:
forum,论坛的公共部分,用户在此处创建讨论和帖子。admin,论坛的私有部分,你作为论坛管理员在此处对Flarum进行配置。
它们共享相同的基础代码,所以只要你学会了如何拓展其中一个,你就能够拓展另一个。
我们在提供新的 TypeScript 支持的同时,提供了一个 tsconfig 配置包,你应该将它作为开发依赖安装,以查看我们的类型申明。 请确保你按照配置包的README文件中的指示配置类型申明支持。 Make sure you follow the instructions in the package's README to configure typings support.
转译和文件结构
本教程的这个部分讲解释编写拓展的必要文件设置。本教程的这个部分讲解释编写拓展的必要文件设置。 再说一次,我们高度推荐使用Flarum CLI来为你创建文件结构。 话虽如此,你仍然应该阅读这一部分以理解文件结构背后的原理。话虽如此,你仍然应该阅读这一部分以理解文件结构背后的原理。
在我们编写JavaScript之前,我们需要配置转译器。 这使得我们可以在Flarum核心代码以及拓展中使用TypeScript和它的魔力。 This allows us to use TypeScript and its magic in Flarum core and extensions.
为了进行转译,你需要一个好的工作环境。不是说在家或者办公室这种环境——你想在厕所写代码我都不管!我指的是安装在你系统上的工具。你需要:
- Node.js (Download)
- A JavaScript package manager: npm (bundled with Node.js), Yarn, or pnpm
- Webpack (
npm install -g webpack)
这可能比较麻烦,因为每个人的系统都不一样。从您正在使用的操作系统,到您已安装的程序版本,到用户访问权限——想想就令人胆寒。 If you run into trouble, tell him I said hi use Google to see if someone has encountered the same error as you and found a solution. If not, ask for help from the Flarum Community.
是时候设置我们的小 JavaScript 转译项目了。是时候设置我们的小 JavaScript 转译项目了。 在你的扩展中创建一个新文件夹,名为 js,然后再丢进去几个新文件。 一个典型拓展的前端结构是这样的:一个典型拓展的前端结构是这样的:
js
├── dist (编译后的js文件保存在此处)
├── src
│ ├── admin
│ └── forum
├── admin.js
├── forum.js
├── package.json
├── tsconfig.json
└── webpack.config.js
package.json
{
"private": true,
"name": "@acme/flarum-hello-world",
"dependencies": {
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^2.0.0",
"flarum-webpack-config": "^3.0.0",
"prettier": "^2.5.1",
"typescript": "^4.5.4",
"typescript-coverage-report": "^0.6.1",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1"
},
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production",
"analyze": "cross-env ANALYZER=true <%= params.jsPackageManager %> run build",
"format": "prettier --write src",
"format-check": "prettier --check src",
"clean-typings": "npx rimraf dist-typings && mkdir dist-typings",
"build-typings": "<%= params.jsPackageManager %> run clean-typings && ([ -e src/@types ] && cp -r src/@types dist-typings/@types || true) && tsc && <%= params.jsPackageManager %> run post-build-typings",
"post-build-typings": "find dist-typings -type f -name '*.d.ts' -print0 | xargs -0 sed -i 's,../src/@types,@types,g'",
"check-typings": "tsc --noEmit --emitDeclarationOnly false",
"check-typings-coverage": "typescript-coverage-report",
}
}
This is a standard JS package-description file, used by JavaScript package managers such as npm, Yarn, and pnpm. 你可以使用它来添加指令、JS依赖和包元数据。我们不是在真正发布一个npm包:这只是用来收集依赖项。
请注意,我们不需要将 flarum/core 或任何flarum扩展作为依赖:它们会在Flarum编译所有前端拓展时自动被打包。
If you use pnpm, you must also declare it in the packageManager field of your package.json:
{
"packageManager": "[email protected]+sha512.9a6f330a95b66446ea088faf1521405a8a01f07fde7124cc9958dfed52d4bb436737e65b08f85f37b46fcba375092558ac51262b816844b22f63406ed166bfee"
}
The value consists of the pnpm version you use, followed by an integrity hash of that release. You don't need to write it by hand: running corepack use pnpm@<version> in the js directory sets the field for you, hash included. This field is read by Corepack to determine which package manager (and version) the project expects. If it is missing, the reusable frontend workflow will fail.
webpack.config.js
const config = require('flarum-webpack-config');
module.exports = config();
Webpack是真正为我们的插件编译并打包所有Javascript (及其依赖) 的系统。 为了使我们的扩展正常工作,它应该使用 官方的 flarum webpack 配置 (在上述例子中展示)。 To work properly, our extensions should use the official flarum webpack config (shown in the above example).
tsconfig.json
{
// Use Flarum's tsconfig as a starting point
"extends": "flarum-tsconfig",
// This will match all .ts, .tsx, .d.ts, .js, .jsx files in your `src` folder
// and also tells your Typescript server to read core's global typings for
// access to `dayjs` and `$` in the global namespace.
"include": [
"src/**/*",
"../vendor/*/*/js/dist-typings/@types/**/*",
"@types/**/*"
],
"compilerOptions": {
// This will output typings to `dist-typings`
"declarationDir": "./dist-typings",
"baseUrl": ".",
"paths": {
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"],
}
}
}
这是一个标准的配置文件,以启用 Flarum 需要的选项支持Typescript。
始终确保您使用此文件的最新版本:https://github.com/flarum/flarum-tsconfig#readme。
Even if you choose not to use TypeScript in your extension, which is supported natively by our Webpack config, it's still recommended to install the flarum-tsconfig package and to include this configuration file so that your IDE can infer types for our core JS.
To get the typings working, you'll need to run composer update in your extension's folder to download the latest copy of Flarum's core into a new vendor folder. 如果您正在使用版本控制系统,请记住不要提交此文件夹。
您可能还需要重启 IDE 的 TypeScript 服务器。 You may also need to restart your IDE's TypeScript server. In Visual Studio Code, you can press F1, then type "Restart TypeScript Server" and hit ENTER. This might take a minute to complete. 完成可能需要一分钟。
admin.js 和 forum.js
These files contain the root of our actual frontend JS. You could put your entire extension here, but that would not be well organized. For this reason, we recommend putting the actual source code in src, and having these files just export the contents of src. For instance: 你可以把你的整个扩展放在这里,但这不是很好的组织方式。 For this reason, we recommend putting the actual source code in src, and having these files just export the contents of src. 就像这样:
// admin.js
export * from './src/admin';
// forum.js
export * from './src/forum';
src
If following the recommendations for admin.js and forum.js, we'll want to have 2 subfolders here: one for admin frontend code, and one for forum frontend code.
If you have components, models, utils, or other code that is shared across both frontends, you may want to create a common subfolder and place it there.
Structure for admin and forum is identical, so we'll just show it for forum here:
src/forum/
├── components/
├── models/
├── utils/
└── index.js
components, models, and utils are directories that contain files where you can define custom components, models, and reusable util helper functions.
请注意,这只是一个建议:没有任何东西强迫您使用这个特定的文件结构(或任何其他文件结构)。
The most important file here is index.js: everything else is just extracting classes and functions into their own files. Let's go over a typical index.js file structure:
import { extend, override } from 'flarum/common/extend';
// We provide our extension code in the form of an "initializer".
// This is a callback that will run after the core has booted.
app.initializers.add('acme-flarum-hello-world', function(app) {
// Your Extension Code Here
console.log("EXTENSION NAME is working!");
});
我们将转到下面可供扩展使用的工具。
导入
You should familiarize yourself with proper syntax for importing js modules, as most extensions larger than a few lines will split their js into multiple files.
Pretty much every Flarum extension will need to import something from Flarum Core.
Like most extensions, core's JS source code is split up into admin, common, and forum folders. You can import the file by prefixing its path in the Flarum core source code with flarum. So admin/components/ExtensionLinkButton is available as flarum/admin/components/ExtensionLinkButton, common/Component is available as flarum/common/Component, and forum/states/PostStreamState is available as flarum/forum/states/PostStreamState.
In some cases, an extension may want to extend code from another flarum extension. This is only possible for extensions which explicitly export their contents. You can use the same import format valid for any third-party extension.
例如,从标签扩展导入:
import TagsPage from 'ext:flarum/tags/forum/components/TagsPage';