Skip to content

Commit

Permalink
chore: enable ts
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Oct 22, 2024
1 parent 020c6b3 commit ff7afdc
Show file tree
Hide file tree
Showing 17 changed files with 1,597 additions and 1,005 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
with:
enable_bundlewatch: false
enable_prettier: true
enable_typescript: false
enable_typescript: true

frontend_directory: ./js
backend_directory: .
Expand Down
37 changes: 22 additions & 15 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
{
"private": true,
"name": "@ianm/synopsis",
"prettier": "@flarum/prettier-config",
"dependencies": {
"flarum-webpack-config": "^2.0.0",
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
"private": true,
"version": "0.0.0",
"devDependencies": {
"flarum-webpack-config": "^2.0.2",
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4",
"prettier": "^3.3.3",
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.3",
"typescript": "^4.5.4",
"typescript-coverage-report": "^0.6.1"
},
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production",
"format": "prettier --write src",
"format-check": "prettier --check src"
"dev": "webpack --mode development --watch",
"build": "webpack --mode production",
"analyze": "cross-env ANALYZER=true npm run build",
"format": "prettier --write src",
"format-check": "prettier --check src",
"clean-typings": "npx rimraf dist-typings && mkdir dist-typings",
"build-typings": "npm run clean-typings && ([ -e src/@types ] && cp -r src/@types dist-typings/@types || true) && tsc && npm 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"
},
"devDependencies": {
"prettier": "^3.0.3"
}
"prettier": "@flarum/prettier-config"
}
30 changes: 30 additions & 0 deletions js/src/@types/shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Tag from 'flarum/tags/common/models/Tag';
import SettingsPage from 'flarum/forum/components/SettingsPage';
import ItemList from 'flarum/common/utils/ItemList';
import type Mithril from 'mithril';
import EditTagModal from 'flarum/tags/admin/components/EditTagModal';
import Stream from 'flarum/common/utils/Stream';

declare module 'flarum/tags/common/models/Tag' {
export default interface Tag {
richExcerpts(): boolean;
excerptLength(): number;
}
}

declare module 'flarum/forum/components/SettingsPage' {
export default interface SettingsPage {
summariesItems(): ItemList<Mithril.Children>;
showSynopsisExcerpts: Stream<boolean>;
showSynopsisExcerptsOnMobile: Stream<boolean>;
showSynopsisExcerptsLoading: boolean;
showSynopsisExcerptsOnMobileLoading: boolean;
}
}

declare module 'flarum/tags/admin/components/EditTagModal' {
export default interface EditTagModal {
richExcerpts: Stream<boolean>;
excerptLength: Stream<number>;
}
}
4 changes: 4 additions & 0 deletions js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Extend from 'flarum/common/extenders';
import commonExtend from '../common/extend';

export default [...commonExtend];
54 changes: 0 additions & 54 deletions js/src/admin/extendEditTagModal.tsx

This file was deleted.

44 changes: 44 additions & 0 deletions js/src/admin/extenders/extendEditTagModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import app from 'flarum/admin/app';
import EditTagModal from 'flarum/tags/admin/components/EditTagModal';
import { extend } from 'flarum/common/extend';
import Stream from 'flarum/common/utils/Stream';

export default function () {
extend(EditTagModal.prototype, 'oninit', function () {
this.richExcerpts = new Stream(this.tag.richExcerpts());
this.excerptLength = new Stream(this.tag.excerptLength());
});

extend(EditTagModal.prototype, 'submitData', function (data) {
data.richExcerpts = this.richExcerpts();
data.excerptLength = this.excerptLength();

return data;
});

extend(EditTagModal.prototype, 'fields', function (items) {
items.add(
'synopsis-excerpt-length',
<div className="Form-group">
<label>{app.translator.trans('ianm-synopsis.admin.settings.excerpt-length.label')}</label>
<input className="FormControl" type="number" min="0" bidi={this.excerptLength} />
<div>{app.translator.trans('ianm-synopsis.admin.settings.excerpt-length.help')}</div>
</div>,
5
);

items.add(
'synopsis-rich-excerpts',
<div className="Form-group">
<div>
<label className="checkbox">
<input type="checkbox" bidi={this.richExcerpts} />
{app.translator.trans('ianm-synopsis.admin.settings.rich-excerpts.label')}
</label>
</div>
<div>{app.translator.trans('ianm-synopsis.admin.settings.rich-excerpts.help')}</div>
</div>,
5
);
});
}
4 changes: 3 additions & 1 deletion js/src/admin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import app from 'flarum/admin/app';
import extendEditTagModal from './extendEditTagModal';
import extendEditTagModal from './extenders/extendEditTagModal';
import typeOptions from './util/typeOptions';

export { default as extend } from './extend';

app.initializers.add('ianm-synopsis', () => {
app.extensionData
.for('ianm-synopsis')
Expand Down
11 changes: 0 additions & 11 deletions js/src/admin/util/typeOptions.js

This file was deleted.

12 changes: 12 additions & 0 deletions js/src/admin/util/typeOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import app from 'flarum/admin/app';
import extractText from 'flarum/common/utils/extractText';

export default function () {
let opts: { [key: string]: string } = {};
opts = ['first', 'last'].reduce((o: { [key: string]: string }, key) => {
o[key] = extractText(app.translator.trans(`ianm-synopsis.admin.settings.${key}-label`));

return o;
}, {});
return opts;
}
8 changes: 8 additions & 0 deletions js/src/common/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Extend from 'flarum/common/extenders';
import Tag from 'flarum/tags/common/models/Tag';

export default [
new Extend.Model(Tag) //
.attribute<boolean>('richExcerpts')
.attribute<number>('excerptLength'),
];
31 changes: 16 additions & 15 deletions js/src/forum/addSummaryExcerpt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ import { extend } from 'flarum/common/extend';
import app from 'flarum/forum/app';
import DiscussionListState from 'flarum/forum/states/DiscussionListState';
import DiscussionListItem from 'flarum/forum/components/DiscussionListItem';
import { truncate } from 'flarum/common/utils/string';
import ItemList from 'flarum/common/utils/ItemList';
/* @ts-ignore */
import Tag from 'flarum/tags/models/Tag';
import Model from 'flarum/common/Model';
import type Mithril from 'mithril';
import Excerpt from './components/Excerpt';

export default function addSummaryExcerpt() {
if (app.initializers.has('flarum-tags')) {
Tag.prototype.richExcerpts = Model.attribute('richExcerpts');
Tag.prototype.excerptLength = Model.attribute('excerptLength');
}

extend(DiscussionListState.prototype, 'requestParams', function (params: any) {
if (app.forum.attribute('synopsis.excerpt_type') === 'first') params.include.push('firstPost');
else params.include.push('lastPost');
extend(DiscussionListState.prototype, 'requestParams', function (params) {
if (typeof params.include === 'string') {
params.include = [params.include];
} else {
params.include = params.include || [];
}

if (app.forum.attribute<string>('synopsis.excerpt_type') === 'first') {
params.include.push('firstPost');
} else {
params.include.push('lastPost');
}
});

extend(DiscussionListItem.prototype, 'infoItems', function (items: ItemList<Mithril.Children>) {
// Skip if we are searching to preserve most relevant post content as excerpt,
// that way we also preserve highlighting of search terms in the most relevant post.
// @ts-expect-error
if (app.forum.attribute('synopsis.disable_when_searching') && app.discussions.params.q) return;

const discussion = this.attrs.discussion;
Expand All @@ -38,9 +39,9 @@ export default function addSummaryExcerpt() {
tag = tags[tags.length - 1];
}

const excerptPost = app.forum.attribute('synopsis.excerpt_type') === 'first' ? discussion.firstPost() : discussion.lastPost();
const excerptLength = typeof tag?.excerptLength() === 'number' ? tag?.excerptLength() : app.forum.attribute('synopsis.excerpt_length');
const richExcerpt = typeof tag?.richExcerpts() === 'number' ? tag?.richExcerpts() : app.forum.attribute('synopsis.rich_excerpts');
const excerptPost = app.forum.attribute<string>('synopsis.excerpt_type') === 'first' ? discussion.firstPost() : discussion.lastPost();
const excerptLength = typeof tag?.excerptLength() === 'number' ? tag?.excerptLength() : app.forum.attribute<number>('synopsis.excerpt_length');
const richExcerpt = typeof tag?.richExcerpts() === 'number' ? tag?.richExcerpts() : app.forum.attribute<boolean>('synopsis.rich_excerpts');
const onMobile = app.session.user ? app.session.user.preferences()?.showSynopsisExcerptsOnMobile : false;

// A length of zero means we don't want a synopsis for this discussion, so do nothing.
Expand Down
29 changes: 16 additions & 13 deletions js/src/forum/addUserPreference.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type Mithril from 'mithril';
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import SettingsPage from 'flarum/forum/components/SettingsPage';
import FieldSet from 'flarum/common/components/FieldSet';
import ItemList from 'flarum/common/utils/ItemList';
import Switch from 'flarum/common/components/Switch';
import Stream from 'flarum/common/utils/Stream';
import type Mithril from 'mithril';

export default function () {
extend(SettingsPage.prototype, 'oninit', function () {
this.showSynopsisExcerpts = Stream(this.user.preferences().showSynopsisExcerpts);
this.showSynopsisExcerptsOnMobile = Stream(this.user.preferences().showSynopsisExcerptsOnMobile);
this.showSynopsisExcerpts = Stream(this.user?.preferences()?.showSynopsisExcerpts);
this.showSynopsisExcerptsOnMobile = Stream(this.user?.preferences()?.showSynopsisExcerptsOnMobile);
});

extend(SettingsPage.prototype, 'settingsItems', function (items: ItemList) {
extend(SettingsPage.prototype, 'settingsItems', function (items) {
items.add(
'synopsis',
FieldSet.component(
Expand All @@ -27,17 +27,20 @@ export default function () {
});

SettingsPage.prototype['summariesItems'] = function () {
const items = new ItemList();
const items = new ItemList<Mithril.Children>();
if (!this.user) return items;

const prefs = this.user.preferences();

items.add(
'synopsis-excerpts',
Switch.component(
{
state: this.user.preferences().showSynopsisExcerpts,
onchange: (value) => {
state: prefs?.showSynopsisExcerpts,
onchange: (value: boolean) => {
this.showSynopsisExcerptsLoading = true;

this.user.savePreferences({ showSynopsisExcerpts: value }).then(() => {
this.user?.savePreferences({ showSynopsisExcerpts: value }).then(() => {
this.showSynopsisExcerptsLoading = false;
m.redraw();
});
Expand All @@ -48,17 +51,17 @@ export default function () {
)
);

if (this.user.preferences().showSynopsisExcerpts) {
if (prefs?.showSynopsisExcerpts) {
items.add(
'synopsis-excerpts-mobile',
Switch.component(
{
state: this.user.preferences().showSynopsisExcerptsOnMobile,
disabled: !this.user.preferences().showSynopsisExcerpts,
onchange: (value) => {
state: prefs?.showSynopsisExcerptsOnMobile,
disabled: !prefs?.showSynopsisExcerpts,
onchange: (value: boolean) => {
this.showSynopsisExcerptsOnMobileLoading = true;

this.user.savePreferences({ showSynopsisExcerptsOnMobile: value }).then(() => {
this.user?.savePreferences({ showSynopsisExcerptsOnMobile: value }).then(() => {
this.showSynopsisExcerptsOnMobileLoading = false;
window.location.reload();
});
Expand Down
4 changes: 4 additions & 0 deletions js/src/forum/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Extend from 'flarum/common/extenders';
import commonExtend from '../common/extend';

export default [...commonExtend];
2 changes: 2 additions & 0 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import app from 'flarum/forum/app';
import addSummaryExcerpt from './addSummaryExcerpt';
import addUserPreference from './addUserPreference';

export { default as extend } from './extend';

export * from './components';

app.initializers.add('ianm-synopsis', () => {
Expand Down
5 changes: 3 additions & 2 deletions js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
// 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/flarum/core/js/dist-typings/@types/**/*"],
"include": ["src/**/*", "../vendor/flarum/core/js/dist-typings/@types/**/*", "../vendor/flarum/tags/js/dist-typings/@types/**/*"],
"compilerOptions": {
// This will output typings to `dist-typings`
"declarationDir": "./dist-typings",
"baseUrl": ".",
"paths": {
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"]
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"],
"flarum/tags/*": ["../vendor/flarum/tags/js/dist-typings/*"]
}
}
}
4 changes: 3 additions & 1 deletion js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('flarum-webpack-config');

module.exports = config();
module.exports = config({
useExtensions: [],
});
Loading

0 comments on commit ff7afdc

Please sign in to comment.