Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Bento amp-wordpress-embed #2

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3a0be40
Intiate amp-wordpress-embed bento component with cli command
ediamin Jun 13, 2021
21bc43d
Add component logic
ediamin Jun 15, 2021
713392e
Refactor component using IframeEmbed
ediamin Jun 15, 2021
6e4c000
Add unit tests for amp-wordpress-embed
ediamin Jun 17, 2021
1a33305
Update documentation
ediamin Jun 17, 2021
ae0d0f5
Update validator protoascii
ediamin Jun 17, 2021
ac23cfc
Add more unit test
ediamin Jun 17, 2021
66c0b6e
Update amp-wordpress-embed.md
ediamin Jun 22, 2021
8a8c54e
Update iframeURL function using useMemo
ediamin Jun 23, 2021
8875cc9
Add more url validation
ediamin Jun 23, 2021
bbf9220
Remove container from supported layout list
ediamin Jun 23, 2021
040ace4
Removed amp true attibute from test
ediamin Jun 23, 2021
d1a18c8
Update test
ediamin Jun 23, 2021
813d8e8
Update tag name in protoascii
ediamin Jun 23, 2021
a3bf0e2
Lint code with amp lint command
ediamin Jun 23, 2021
41bce32
Update storybook example
ediamin Jun 24, 2021
b3d1fd4
Use forceChangeHeight instead of attemptChangeHeight
ediamin Jun 24, 2021
b4661c7
Add westonruter as code owner
ediamin Jun 24, 2021
f1d8c10
Update the go to link logic
ediamin Jun 24, 2021
945d225
Use simple height calculation
ediamin Jun 24, 2021
afb4fb4
Run prettify command on local
ediamin Jun 24, 2021
2e9f7c5
Use useEffect instead of useLayoutEffect to avoid re-render
ediamin Jun 24, 2021
083747e
Update unit tests
ediamin Jun 24, 2021
02e8580
Add npm true in bundle config
ediamin Jun 24, 2021
1c5954b
Use WordPress per capital_P_dangit rule
ediamin Jun 25, 2021
7e39059
Make height attribute non-optional
ediamin Jun 25, 2021
0db0851
Update layout attribute in example
ediamin Jun 25, 2021
805fbd1
Update amp-wordpress-embed.md
ediamin Jun 25, 2021
dd4ce93
Use WordPress instead of Wordpress
ediamin Jun 25, 2021
b9a12cf
Add overflow button in example snippet
ediamin Jun 25, 2021
901bf56
Added common attributes in doc
ediamin Jun 26, 2021
6883738
Add ediamin as code owner
ediamin Jun 28, 2021
b7fd65a
Merge branch 'main' into add/amp-wordpress-embed
ediamin Jun 29, 2021
96c78df
Remove children props from the component
ediamin Jun 29, 2021
d77f9e5
Merge branch 'main' into add/amp-wordpress-embed
ediamin Jul 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build-system/compile/bundles.config.extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,15 @@
"version": "0.1",
"latestVersion": "0.1"
},
{
"name": "amp-wordpress-embed",
"version": "1.0",
"latestVersion": "1.0",
"options": {
"npm": true,
"wrappers": "bento"
ediamin marked this conversation as resolved.
Show resolved Hide resolved
}
},
{
"name": "amp-yotpo",
"version": "0.1",
Expand Down
49 changes: 49 additions & 0 deletions extensions/amp-wordpress-embed/1.0/amp-wordpress-embed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {BaseElement} from './base-element';
import {dict} from '#core/types/object';
import {isExperimentOn} from '#experiments';
import {userAssert} from '../../../src/log';

/** @const {string} */
const TAG = 'amp-wordpress-embed';

class AmpWordPressEmbed extends BaseElement {
/** @override */
isLayoutSupported(layout) {
userAssert(
isExperimentOn(this.win, 'bento') ||
isExperimentOn(this.win, 'bento-wordpress-embed'),
'expected global "bento" or specific "bento-wordpress-embed" experiment to be enabled'
);
return super.isLayoutSupported(layout);
}

/** @override */
init() {
return dict({
'requestResize': (height) =>
this.attemptChangeHeight(height).catch(() => {
/* ignore failures */
}),
});
}
}

AMP.extension(TAG, '1.0', (AMP) => {
AMP.registerElement(TAG, AmpWordPressEmbed);
});
35 changes: 35 additions & 0 deletions extensions/amp-wordpress-embed/1.0/base-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {PreactBaseElement} from '#preact/base-element';
import {WordPressEmbed} from './component';

export class BaseElement extends PreactBaseElement {}

/** @override */
BaseElement['Component'] = WordPressEmbed;

/** @override */
BaseElement['props'] = {
url: {attr: 'data-url', default: ''},
title: {attr: 'title'},
};

/** @override */
BaseElement['layoutSizeDefined'] = true;

/** @override */
BaseElement['usesShadowDom'] = true;
133 changes: 133 additions & 0 deletions extensions/amp-wordpress-embed/1.0/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Preact from '#preact';
import {IframeEmbed} from '#preact/component/iframe';
import {addParamToUrl} from '../../../src/url';
import {dict} from '#core/types/object';
import {forwardRef} from '#preact/compat';
import {getData} from '../../../src/event-helper';

const {useCallback, useEffect, useMemo, useRef, useState} = Preact;

const NO_HEIGHT_STYLE = dict();

/**
* @param {!WordPressEmbedDef.Props} props
* @param {{current: ?WordPressEmbedDef.Api}} ref
* @return {PreactDef.Renderable}
*/
function WordPressEmbedWithRef(
{requestResize, title = 'WordPressEmbed', url, ...rest},
ref
) {
const [heightStyle, setHeightStyle] = useState(NO_HEIGHT_STYLE);
const [opacity, setOpacity] = useState(0);
const contentRef = useRef(null);
const [win, setWin] = useState(null);

const iframeURL = useMemo(() => {
return addParamToUrl(url, 'embed', 'true');
}, [url]);

const matchesMessagingOrigin = useCallback(
(testURL) => {
const embeddedUrl = new URL(url);
const checkedUrl = new URL(testURL);
return embeddedUrl.origin === checkedUrl.origin;
},
[url]
);

const messageHandler = useCallback(
(event) => {
const data = getData(event);

switch (data.message) {
case 'height':
if (typeof data.value === 'number') {
const height = data.value;
if (requestResize) {
requestResize(height);
}
setHeightStyle(dict({'height': height}));
setOpacity(1);
}
break;
case 'link':
// Only follow a link message for the currently-active iframe if the link is for the same origin.
// This replicates a constraint in WordPress's wp.receiveEmbedMessage() function.
if (matchesMessagingOrigin(data.value) && win) {
win.top.location.href = data.value;
}
break;
}
},
[requestResize, matchesMessagingOrigin, win]
);

useEffect(() => {
setWin(contentRef.current?.ownerDocument?.defaultView);
}, []);

// Checking for valid props
if (!checkProps(url)) {
return null;
}

return (
<IframeEmbed
allowTransparency
iframeStyle={{opacity}}
matchesMessagingOrigin={matchesMessagingOrigin}
messageHandler={messageHandler}
ref={ref}
contentRef={contentRef}
src={iframeURL}
wrapperStyle={heightStyle}
title={title}
{...rest}
/>
);
}

const WordPressEmbed = forwardRef(WordPressEmbedWithRef);
WordPressEmbed.displayName = 'WordPressEmbed'; // Make findable for tests.
export {WordPressEmbed};

/**
* Verify required props and throw error if necessary.
* @param {string|undefined} url
* @return {boolean} true on valid
*/
function checkProps(url) {
// Perform manual checking as assertion is not available for Bento: Issue #32739
try {
new URL(url);
return true;
} catch (error) {
displayWarning('Please provide a valid url');
return false;
}
}

/**
* @param {?string} message
*/
function displayWarning(message) {
console /*OK*/
.warn(message);
}
37 changes: 37 additions & 0 deletions extensions/amp-wordpress-embed/1.0/component.type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/** @externs */

/** @const */
var WordPressEmbedDef = {};

/**
* @typedef {{
* url: string,
* title: (string|undefined),
* requestResize: (function(number):*|undefined),
* loading: (string|undefined),
* onReadyState: (function(string, *=)|undefined),
* }}
*/
WordPressEmbedDef.Props;

/** @constructor */
WordPressEmbedDef.Api = function () {};

/** @type {string} */
WordPressEmbedDef.Api.prototype.readyState;
57 changes: 57 additions & 0 deletions extensions/amp-wordpress-embed/1.0/storybook/Basic.amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Preact from '#preact';
import {number, text, withKnobs} from '@storybook/addon-knobs';
import {withAmp} from '@ampproject/storybook-addon';

export default {
title: 'amp-wordpress-embed-1_0',
decorators: [withKnobs, withAmp],

parameters: {
extensions: [{name: 'amp-wordpress-embed', version: '1.0'}],
experiments: ['bento'],
},
};

export const BasicEmbedExample = () => {
const url = text(
'url',
'https://wordpress.org/news/2021/06/gutenberg-highlights'
);
const width = number('width', 500);
const height = number('height', 200);
const layout = text('layout', 'fixed');

return (
<>
<amp-wordpress-embed
data-url={url}
width={width}
height={height}
layout={layout}
>
<button overflow>Load more</button>
</amp-wordpress-embed>
<p>text below</p>
</>
);
};

BasicEmbedExample.story = {
name: 'Basic example',
};
36 changes: 36 additions & 0 deletions extensions/amp-wordpress-embed/1.0/storybook/Basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Preact from '#preact';
import {WordPressEmbed} from '../component';
import {number, text, withKnobs} from '@storybook/addon-knobs';

export default {
title: 'WordPressEmbed',
component: WordPressEmbed,
decorators: [withKnobs],
};

export const _default = () => {
const url = text(
'url',
'https://wordpress.org/news/2021/06/gutenberg-highlights'
);
const width = number('width', 500);
const height = number('height', 200);

return <WordPressEmbed url={url} style={{width, height}} />;
};
Loading