Skip to content

Commit

Permalink
Merge pull request #539 from uploadcare/fix/wait-for-ctx-name-attr
Browse files Browse the repository at this point in the history
Fix/wait-for-ctx-name-attr
  • Loading branch information
nd0ut authored Oct 12, 2023
2 parents 5d01ac9 + f9d62d4 commit 5c78438
Show file tree
Hide file tree
Showing 16 changed files with 446 additions and 19 deletions.
19 changes: 18 additions & 1 deletion abstract/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { sharedConfigKey } from './sharedConfigKey.js';
import { toKebabCase } from '../utils/toKebabCase.js';
import { warnOnce } from '../utils/warnOnce.js';
import { getPluralForm } from '../utils/getPluralForm.js';
import { waitForAttribute } from '../utils/waitForAttribute.js';

const TAG_PREFIX = 'lr-';

Expand All @@ -16,6 +17,7 @@ export class Block extends BaseComponent {
/** @type {string | null} */
static StateConsumerScope = null;
static className = '';
requireCtxName = false;
allowCustomTemplate = true;

init$ = blockCtx();
Expand Down Expand Up @@ -137,7 +139,22 @@ export class Block extends BaseComponent {
this.constructor['template'] = null;
this.processInnerHtml = true;
}
super.connectedCallback();
if (this.requireCtxName) {
waitForAttribute({
element: this,
attribute: 'ctx-name',
onSuccess: () => {
// async wait for ctx-name attribute to be set, needed for Angular because it sets attributes after mount
// TODO: should be moved to the symbiote core
super.connectedCallback();
},
onTimeout: () => {
console.error('Attribute `ctx-name` is required and it is not set.');
},
});
} else {
super.connectedCallback();
}
}

disconnectedCallback() {
Expand Down
1 change: 1 addition & 0 deletions abstract/SolutionBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ShadowWrapper } from '../blocks/ShadowWrapper/ShadowWrapper.js';
import { uploaderBlockCtx } from './CTX.js';

export class SolutionBlock extends ShadowWrapper {
requireCtxName = true;
init$ = uploaderBlockCtx(this);
_template = null;

Expand Down
2 changes: 1 addition & 1 deletion abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class UploaderBlock extends ActivityBlock {
fileSize: file.size,
silentUpload: silent ?? false,
source: source ?? UploadSource.API,
fullPath,
fullPath: fullPath ?? null,
});
}

Expand Down
1 change: 1 addition & 0 deletions blocks/Config/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const attrStateMapping = /** @type {Record<keyof import('../../types').ConfigAtt

export class Config extends Block {
ctxOwner = true;
requireCtxName = true;

constructor() {
super();
Expand Down
1 change: 1 addition & 0 deletions blocks/DataOutput/DataOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { uploadFileGroup } from '@uploadcare/upload-client';

export class DataOutput extends UploaderBlock {
processInnerHtml = true;
requireCtxName = true;

init$ = {
...this.init$,
Expand Down
15 changes: 9 additions & 6 deletions blocks/ShadowWrapper/ShadowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { Block } from '../../abstract/Block.js';
import { waitForAttribute } from '../../utils/waitForAttribute.js';

const CSS_ATTRIBUTE = 'css-src';

Expand All @@ -22,17 +23,18 @@ export function shadowed(Base) {
return class extends Base {
renderShadow = true;
pauseRender = true;
requireCtxName = true;

shadowReadyCallback() {}

initCallback() {
super.initCallback();
this.setAttribute('hidden', '');

// async wait for attributes to be set, needed for Angular because it sets attributes after constructor
setTimeout(() => {
let href = this.getAttribute(CSS_ATTRIBUTE);
if (href) {
waitForAttribute({
element: this,
attribute: CSS_ATTRIBUTE,
onSuccess: (href) => {
this.attachShadow({
mode: 'open',
});
Expand All @@ -53,11 +55,12 @@ export function shadowed(Base) {
};
// @ts-ignore TODO: fix this
this.shadowRoot.prepend(link);
} else {
},
onTimeout: () => {
console.error(
'Attribute `css-src` is required and it is not set. See migration guide: https://uploadcare.com/docs/file-uploader/migration-to-0.25.0/'
);
}
},
});
}
};
Expand Down
4 changes: 3 additions & 1 deletion blocks/UploadCtxProvider/UploadCtxProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UploaderBlock } from '../../abstract/UploaderBlock.js';

export class UploadCtxProvider extends UploaderBlock {}
export class UploadCtxProvider extends UploaderBlock {
requireCtxName = true;
}
Loading

0 comments on commit 5c78438

Please sign in to comment.