1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 16:36:47 +02:00

feat(emoji): allow the user to set the cdn address (#3908)

This commit is contained in:
青石
2024-09-20 16:40:23 +08:00
committed by GitHub
parent 9b9fd76045
commit c5b4ff7b27
8 changed files with 50 additions and 16 deletions

View File

@@ -11,6 +11,9 @@ use Flarum\Extend;
use s9e\TextFormatter\Configurator; use s9e\TextFormatter\Configurator;
return [ return [
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
(new Extend\Frontend('forum')) (new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js') ->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/less/forum.less') ->css(__DIR__.'/less/forum.less')
@@ -30,4 +33,8 @@ return [
}), }),
new Extend\Locales(__DIR__.'/locale'), new Extend\Locales(__DIR__.'/locale'),
(new Extend\Settings)
->serializeToForum('flarum-emoji.cdn', 'flarum-emoji.cdn')
->default('flarum-emoji.cdn', 'https://cdn.jsdelivr.net/gh/twitter/twemoji@[version]/assets/'),
]; ];

View File

@@ -0,0 +1 @@
export * from './src/admin';

View File

@@ -0,0 +1,13 @@
import app from 'flarum/admin/app';
import { version } from '../common/cdn';
app.initializers.add('flarum-emoji', () => {
app.extensionData.for('flarum-emoji').registerSetting({
setting: 'flarum-emoji.cdn',
type: 'text',
label: app.translator.trans('flarum-emoji.admin.settings.cdn_label'),
help: app.translator.trans('flarum-emoji.admin.settings.cdn_help', {
version: version,
}),
});
});

View File

@@ -0,0 +1,7 @@
import twemoji from 'twemoji';
export const version = /([0-9]+).[0-9]+.[0-9]+/g.exec(twemoji.base)[1];
export default function () {
return app.forum.attribute('flarum-emoji.cdn').replace('[version]', version);
}

View File

@@ -6,7 +6,7 @@ import AutocompleteReader from 'flarum/common/utils/AutocompleteReader';
import AutocompleteDropdown from './fragments/AutocompleteDropdown'; import AutocompleteDropdown from './fragments/AutocompleteDropdown';
import getEmojiIconCode from './helpers/getEmojiIconCode'; import getEmojiIconCode from './helpers/getEmojiIconCode';
import cdn from './cdn'; import cdn from '../common/cdn';
export default function addComposerAutocomplete() { export default function addComposerAutocomplete() {
let emojiMap = null; let emojiMap = null;
@@ -40,6 +40,7 @@ export default function addComposerAutocomplete() {
extend('flarum/common/components/TextEditor', 'buildEditorParams', function (params) { extend('flarum/common/components/TextEditor', 'buildEditorParams', function (params) {
const emojiKeys = Object.keys(emojiMap); const emojiKeys = Object.keys(emojiMap);
const resolvedCdn = cdn();
const autocompleteReader = new AutocompleteReader(':'); const autocompleteReader = new AutocompleteReader(':');
@@ -75,7 +76,7 @@ export default function addComposerAutocomplete() {
emojiDropdown.setIndex($(this).parent().index() - 1); emojiDropdown.setIndex($(this).parent().index() - 1);
}} }}
> >
<img alt={emoji} className="emoji" draggable="false" loading="lazy" src={`${cdn}72x72/${code}.png`} title={name} /> <img alt={emoji} className="emoji" draggable="false" loading="lazy" src={`${resolvedCdn}72x72/${code}.png`} title={name} />
</button> </button>
</Tooltip> </Tooltip>
); );

View File

@@ -1,5 +0,0 @@
import twemoji from 'twemoji';
export const version = /([0-9]+).[0-9]+.[0-9]+/g.exec(twemoji.base)[1];
export default `https://cdn.jsdelivr.net/gh/twitter/twemoji@${version}/assets/`;

View File

@@ -3,14 +3,16 @@ import twemoji from 'twemoji';
import { override } from 'flarum/common/extend'; import { override } from 'flarum/common/extend';
import Post from 'flarum/common/models/Post'; import Post from 'flarum/common/models/Post';
import base from './cdn'; import cdn from '../common/cdn';
const options = { function options() {
base, return {
attributes: () => ({ base: cdn(),
loading: 'lazy', attributes: () => ({
}), loading: 'lazy',
}; }),
};
}
/** /**
* Parses an HTML string into a `<body>` node containing the HTML content. * Parses an HTML string into a `<body>` node containing the HTML content.
@@ -40,7 +42,7 @@ export default function renderEmoji() {
// element. This gets stripped below. // element. This gets stripped below.
// //
// See https://github.com/flarum/core/issues/2958 // See https://github.com/flarum/core/issues/2958
const emojifiedDom = twemoji.parse(parseHTML(contentHtml), options); const emojifiedDom = twemoji.parse(parseHTML(contentHtml), options());
// Steal the HTML string inside the emojified DOM `<body>` tag. // Steal the HTML string inside the emojified DOM `<body>` tag.
this.emojifiedContentHtml = emojifiedDom.innerHTML; this.emojifiedContentHtml = emojifiedDom.innerHTML;
@@ -54,6 +56,6 @@ export default function renderEmoji() {
override(s9e.TextFormatter, 'preview', (original, text, element) => { override(s9e.TextFormatter, 'preview', (original, text, element) => {
original(text, element); original(text, element);
twemoji.parse(element, options); twemoji.parse(element, options());
}); });
} }

View File

@@ -4,6 +4,14 @@ flarum-emoji:
# UNIQUE KEYS - The following keys are used in only one location each. # UNIQUE KEYS - The following keys are used in only one location each.
## ##
# Translations in this namespace are used by the admin interface.
admin:
# These translations are used in the Settings page of the admin interface.
settings:
cdn_label: CDN mirror address
cdn_help: "e.g. https://cdn.jsdelivr.net/gh/twitter/twemoji@[version]/assets/, The current version is: {version}"
# Translations in this namespace are used by the forum user interface. # Translations in this namespace are used by the forum user interface.
forum: forum: