Skip to content

Commit

Permalink
Merge pull request #5080 from dodona-edu/chore/update-lit-3
Browse files Browse the repository at this point in the history
Upgrade to lit 3.0
  • Loading branch information
jorg-vr authored Oct 26, 2023
2 parents 0e77184 + 09ffc9d commit 1c1f907
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ShadowlessLitElement } from "components/meta/shadowless_lit_element";
import { customElement, property } from "lit/decorators.js";
import { html, TemplateResult } from "lit";
import { html, TemplateResult, PropertyValues } from "lit";
import "components/annotations/annotations_cell";
import "components/annotations/hidden_annotations_dot";
import { i18nMixin } from "components/meta/i18n_mixin";
import { initTooltips } from "utilities";
import { PropertyValues } from "@lit/reactive-element";
import { userState } from "state/Users";
import { annotationState } from "state/Annotations";
import { userAnnotationState } from "state/UserAnnotations";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ShadowlessLitElement } from "components/meta/shadowless_lit_element";
import { customElement, property } from "lit/decorators.js";
import { html, TemplateResult } from "lit";
import { html, TemplateResult, PropertyValues } from "lit";
import { MachineAnnotation, machineAnnotationState } from "state/MachineAnnotations";
import { UserAnnotation, userAnnotationState } from "state/UserAnnotations";
import { i18nMixin } from "components/meta/i18n_mixin";
import { PropertyValues } from "@lit/reactive-element/development/reactive-element";
import { initTooltips } from "utilities";
import { annotationState, compareAnnotationOrders } from "state/Annotations";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ShadowlessLitElement extends LitElement {
}

// don't use shadow dom
createRenderRoot(): Element {
createRenderRoot(): HTMLElement {
return this;
}
}
2 changes: 1 addition & 1 deletion app/assets/javascripts/punchcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function initPunchcard(url: string): void {
const xAxis = d3.axisBottom(x)
.ticks(24)
.tickSize(0)
.tickFormat(hour => `${hour < 10 ? "0" : ""}${hour}:00`)
.tickFormat(hour => `${(hour as number) < 10 ? "0" : ""}${hour}:00`)
.tickPadding(10);
const yAxis = d3.axisLeft(y)
.ticks(7)
Expand Down
65 changes: 11 additions & 54 deletions app/assets/javascripts/state/state_system/StateProperty.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,25 @@
import { State } from "./State";
import { ClassElement, Constructor } from "@lit/reactive-element/decorators/base.js";

/**
* This function is used to decorate a property of a State class.
* It will make the property reactive and will dispatch a StateEvent when the property changes.
* It will also record every read of the property to the stateRecorder.
* @param stateClass
* @param property
* Function for decorating a property that is compatible with both TypeScript decorators.
* It will keep track of readers and dispatch events when the property is changed.
*
* @returns {void}
*/
function finisher(stateClass: typeof State, property: PropertyKey): void {
const key = typeof property === "symbol" ? Symbol() : `__${property}`;
const currentVal = stateClass.prototype[property];
Object.defineProperty(stateClass.prototype, property, {
export const stateProperty = (proto: State, name?: PropertyKey): void => {
const key = typeof name === "symbol" ? Symbol() : `__${name}`;
const currentVal = proto[name];
Object.defineProperty(proto, name, {
get(): unknown {
this.recordRead(property);
this.recordRead(name);
return this[key];
},
set(value: unknown) {
this[key] = value;
this.dispatchStateEvent(property);
this.dispatchStateEvent(name);
},
configurable: true,
enumerable: true,
});
stateClass.prototype[key] = currentVal;
}

/**
* Function for decorating a property that is compatible with both TypeScript and Babel decorators.
* It will apply the `finisher` function to the property.
*
* @returns {ClassElement|void}
*/
export const stateProperty = (
protoOrDescriptor: State | ClassElement,
name?: PropertyKey
// Note TypeScript requires the return type to be `void|any`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): void | any => {
if (name !== undefined) {
// TypeScript / Babel legacy mode
const ctor = (protoOrDescriptor as State).constructor as typeof State;
Object.defineProperty(protoOrDescriptor, name, {
writable: true,
configurable: true,
enumerable: true
});
finisher(ctor, name);
} else {
// Babel standard mode
const element = protoOrDescriptor as ClassElement;
return {
kind: "field",
placement: "prototype",
key: Symbol(),
descriptor: {},
finisher: function <State> (ctor: Constructor<State>) {
finisher(ctor as typeof State, element.key);
},
initializer(this: {[key: symbol]: unknown}) {
if (typeof element.initializer === "function") {
this[element.key] = element.initializer.call(this);
}
},
};
}
proto[key] = currentVal;
};
11 changes: 0 additions & 11 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,17 @@ module.exports = function (api) {
exclude: ["transform-typeof-symbol"]
}
],
["@babel/preset-typescript", {
allExtensions: true,
isTSX: true
}]
].filter(Boolean),
ignore: [],
plugins: [
"babel-plugin-macros",
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-transform-for-of",
isTestEnv && "babel-plugin-dynamic-import-node",
isDevelopmentEnv && ["istanbul", { include: ["app/assets/javascripts/**/*.{js,ts}"], coverageGlobalScopeFunc: false, coverageGlobalScope: "window" }],
"@babel/plugin-transform-destructuring",
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
["@babel/plugin-proposal-object-rest-spread", { useBuiltIns: true }],
["@babel/plugin-transform-runtime", {
helpers: false,
regenerator: true,
corejs: false
}],
["@babel/plugin-transform-regenerator", { async: false }],
"@babel/plugin-transform-class-properties"
].filter(Boolean)
};
};
9 changes: 8 additions & 1 deletion config/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ const config = {
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx|)$/,
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: function (modulePath) {
return modulePath.endsWith('.ts') && !modulePath.endsWith('test.ts');
},
exclude: /node_modules/,
use: ["babel-loader", "ts-loader"],
},
],
},
optimization: {
Expand Down
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
},
"dependencies": {
"@babel/core": "^7.23.2",
"@babel/plugin-proposal-decorators": "^7.23.2",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/plugin-transform-class-properties": "^7.22.5",
"@babel/plugin-transform-runtime": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@babel/preset-typescript": "^7.23.2",
"@dodona/papyros": "^1.0.1",
"@dodona/pyodide-trace-library": "^2.0.7",
"@popperjs/core": "^2.11.8",
"@rails/activestorage": "^7.1.1",
"@rails/ujs": "^7.1.1",
"@types/ace": "^0.0.50",
"@types/bootstrap": "^5.2.8",
"@types/d3": "^7.4.2",
"@types/dragula": "^3.7.3",
"@types/iframe-resizer": "^3.5.11",
"@types/jest": "^27.5.0",
"@types/serviceworker": "^0.0.76",
"babel-loader": "^9.1.3",
"babel-plugin-macros": "^3.1.0",
"bootstrap": "5.3.2",
Expand All @@ -37,24 +39,19 @@
"fscreen": "^1.2.0",
"glightbox": "^3.2.0",
"iframe-resizer": "^4.3.7",
"lit": "2.8.0",
"lit": "^3.0.0",
"node-polyglot": "^2.5.0",
"sass": "^1.69.4",
"tippy.js": "^6.3.7",
"typescript": "^4.9.5",
"ts-loader": "^9.5.0",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"devDependencies": {
"@open-wc/testing-helpers": "^2.3.0",
"@testing-library/dom": "^9.3.3",
"@testing-library/user-event": "^14.5.1",
"@types/ace": "^0.0.50",
"@types/bootstrap": "^5.2.8",
"@types/dragula": "^3.7.3",
"@types/iframe-resizer": "^3.5.11",
"@types/jest": "^27.5.0",
"@types/serviceworker": "^0.0.76",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"babel-plugin-istanbul": "^6.1.1",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"allowUmdGlobalAccess": true,
"resolveJsonModule": true,
},
"exclude": ["**/*.spec.ts", "node_modules", "public", "data"],
"exclude": ["**/*.spec.ts","**/*.test.ts", "node_modules", "public", "data"],
"compileOnSave": false
}
Loading

0 comments on commit 1c1f907

Please sign in to comment.