Skip to content

Commit

Permalink
feat(shadow-styles): eliminating shadow styles (#646)
Browse files Browse the repository at this point in the history
* feat(shadow-styles): deleted ShadowWrapper

* feat(shadow-styles): deleted :host

* feat(shadow-styles): replaced shadowReadyCallback with initCallback

* feat(shadow-styles): added class minimal for file-uploader-minimal

* fix: fixed localization initialization

* feat(shadow-styles): added class for lr-file-uploader-inline

* feat: added class for minimal and inline

* fix: ctxOwner for EditorImageCropper

* feat: deleted pauseRender

* fix: add state keys if they're not present at the moment

---------

Co-authored-by: nd0ut <[email protected]>
Co-authored-by: Aleksandr Grenishin <[email protected]>
  • Loading branch information
3 people authored May 7, 2024
1 parent 1e975c1 commit 984dbda
Show file tree
Hide file tree
Showing 24 changed files with 209 additions and 262 deletions.
15 changes: 13 additions & 2 deletions abstract/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { blockCtx } from './CTX.js';
import { LocaleManager, localeStateKey } from './LocaleManager.js';
import { l10nProcessor } from './l10nProcessor.js';
import { sharedConfigKey } from './sharedConfigKey.js';
import { initialConfig } from '../blocks/Config/initialConfig.js';

const TAG_PREFIX = 'lr-';

Expand Down Expand Up @@ -172,7 +173,6 @@ export class Block extends BaseComponent {
if (!this.has('*eventEmitter')) {
this.add('*eventEmitter', new EventEmitter(this.debugPrint.bind(this)));
}

if (!this.has('*localeManager')) {
this.add('*localeManager', new LocaleManager(this));
}
Expand Down Expand Up @@ -264,6 +264,9 @@ export class Block extends BaseComponent {
return false;
}
const sharedKey = sharedConfigKey(/** @type {keyof import('../types').ConfigType} */ (key));
if (!this.has(sharedKey)) {
this.add(sharedKey, initialConfig[/** @type {keyof import('../types').ConfigType} */ (key)]);
}
this.$[sharedKey] = value;
return true;
},
Expand All @@ -272,6 +275,10 @@ export class Block extends BaseComponent {
* @param {keyof import('../types').ConfigType} key
*/
get: (obj, key) => {
const sharedKey = sharedConfigKey(key);
if (!this.has(sharedKey)) {
this.add(sharedKey, initialConfig[key]);
}
return this.$[sharedConfigKey(key)];
},
});
Expand All @@ -285,7 +292,11 @@ export class Block extends BaseComponent {
* @param {(value: import('../types').ConfigType[T]) => void} callback
*/
subConfigValue(key, callback) {
this.sub(sharedConfigKey(key), callback);
const sharedKey = sharedConfigKey(key);
if (!this.has(sharedKey)) {
this.add(sharedKey, initialConfig[key]);
}
this.sub(sharedKey, callback);
}

/** @param {unknown[]} args */
Expand Down
5 changes: 4 additions & 1 deletion abstract/LocaleManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
import { debounce } from '../blocks/utils/debounce.js';
import { resolveLocaleDefinition } from './localeRegistry.js';
import { default as en } from '../locales/file-uploader/en.js';

/** @param {string} key */
export const localeStateKey = (key) => `*l10n/${key}`;
Expand Down Expand Up @@ -32,7 +33,9 @@ export class LocaleManager {
constructor(blockInstance) {
this._blockInstance = blockInstance;

blockInstance.add(localeStateKey('locale-id'), DEFAULT_LOCALE, false);
for (let [key, value] of Object.entries(en)) {
this._blockInstance.add(localeStateKey(key), value, false);
}

setTimeout(() => {
blockInstance.subConfigValue('localeName', async (localeName) => {
Expand Down
4 changes: 2 additions & 2 deletions abstract/SolutionBlock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ShadowWrapper } from '../blocks/ShadowWrapper/ShadowWrapper.js';
import { uploaderBlockCtx } from './CTX.js';
import { Block } from './Block.js';

export class SolutionBlock extends ShadowWrapper {
export class SolutionBlock extends Block {
requireCtxName = true;
init$ = uploaderBlockCtx(this);
_template = null;
Expand Down
81 changes: 41 additions & 40 deletions abstract/l10nProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,52 @@ export function l10nProcessor(fr, fnCtx) {
}
}

// If the key is present in the global context, use it
const stateKey = localeStateKey(key);

if (fnCtx.has(stateKey)) {
fnCtx.sub(stateKey, () => {
key = /** @type {string} */ (key);
if (useAttribute) {
el.setAttribute(elProp, fnCtx.l10n(key));
} else {
el[/** @type {'textContent'} */ (elProp)] = fnCtx.l10n(key);
// Check if the key is present in the local context
const localCtxKey = key;
if (fnCtx.has(localCtxKey)) {
fnCtx.sub(localCtxKey, (mappedKey) => {
if (!mappedKey) {
return;
}
// Store the subscription in a temporary map to be able to unsubscribe later
if (!fnCtx.l10nProcessorSubs.has(localCtxKey)) {
fnCtx.l10nProcessorSubs.set(localCtxKey, new Set());
}
const keySubs = fnCtx.l10nProcessorSubs.get(localCtxKey);
keySubs?.forEach(
/** @param {{ remove: () => void }} sub */
(sub) => {
sub.remove();
keySubs.delete(sub);
fnCtx.allSubs.delete(sub);
},
);
// We don't need the leading * in the key because we use the key as a local context key relative to the global state
const nodeStateKey = localeStateKey(mappedKey).replace('*', '');
// Subscribe on the global l10n key change
const sub = fnCtx.nodeCtx.sub(nodeStateKey, () => {
el[/** @type {'textContent'} */ (elProp)] = fnCtx.l10n(mappedKey);
});
keySubs?.add(sub);
// Store the subscription in the global context to make able Symbiote to unsubscribe it on destroy
fnCtx.allSubs.add(sub);
el.removeAttribute('l10n');
});
el.removeAttribute('l10n');
return;
}

// Otherwise, assume the key is present in the local context
const localCtxKey = key;
fnCtx.sub(localCtxKey, (mappedKey) => {
if (!mappedKey) {
return;
}
// Store the subscription in a temporary map to be able to unsubscribe later
if (!fnCtx.l10nProcessorSubs.has(localCtxKey)) {
fnCtx.l10nProcessorSubs.set(localCtxKey, new Set());
// Otherwise, assume the key is in the global context
const stateKey = localeStateKey(key);
if (!fnCtx.has(stateKey)) {
fnCtx.add(stateKey, '');
}
fnCtx.sub(stateKey, () => {
key = /** @type {string} */ (key);
if (useAttribute) {
el.setAttribute(elProp, fnCtx.l10n(key));
} else {
el[/** @type {'textContent'} */ (elProp)] = fnCtx.l10n(key);
}
const keySubs = fnCtx.l10nProcessorSubs.get(localCtxKey);
keySubs?.forEach(
/** @param {{ remove: () => void }} sub */
(sub) => {
sub.remove();
keySubs.delete(sub);
fnCtx.allSubs.delete(sub);
},
);
// We don't need the leading * in the key because we use the key as a local context key relative to the global state
const nodeStateKey = localeStateKey(mappedKey).replace('*', '');
// Subscribe on the global l10n key change
const sub = fnCtx.nodeCtx.sub(nodeStateKey, () => {
el[/** @type {'textContent'} */ (elProp)] = fnCtx.l10n(mappedKey);
});
keySubs?.add(sub);
// Store the subscription in the global context to make able Symbiote to unsubscribe it on destroy
fnCtx.allSubs.add(sub);
el.removeAttribute('l10n');
});
el.removeAttribute('l10n');
});
}
1 change: 1 addition & 0 deletions blocks/CloudImageEditor/src/EditorImageCropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function validateCrop(crop) {
}

export class EditorImageCropper extends Block {
ctxOwner = true;
constructor() {
super();

Expand Down
93 changes: 0 additions & 93 deletions blocks/ShadowWrapper/ShadowWrapper.js

This file was deleted.

Loading

0 comments on commit 984dbda

Please sign in to comment.