这是indexloc提供的服务,不要输入任何密码
Skip to content

Add support for AMP Stories #98

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

Merged
merged 5 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"presets": [
[ "env", {
"modules": false,
"targets": {
"browsers": [
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Safari versions",
"last 2 Edge versions",
"last 2 Opera versions",
"last 2 iOS versions",
"last 1 Android version",
"last 1 ChromeAndroid version",
"ie 11",
"> 1%"
]
}
} ]
],
"plugins": [
"transform-object-rest-spread",
[ "transform-react-jsx", {
"pragma": "wp.element.createElement"
} ]
],
"env": {
"default": {
"plugins": [
"lodash",
"transform-runtime"
]
},
}
}
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/*.min.js
**/*.build.js
**/node_modules/**
**/vendor/**
build
node_modules
Gruntfile.js
262 changes: 262 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"root": true,
"parser": "babel-eslint",
"extends": [
"wordpress",
"plugin:react/recommended",
"plugin:jsx-a11y/recommended",
"plugin:jest/recommended"
],
"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true,
"jest/globals": true
},
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"globals": {
"wp": true,
"wpApiSettings": true,
"window": true,
"document": true
},
"plugins": [
"wordpress",
"react",
"jsx-a11y",
"jest"
],
"settings": {
"react": {
"pragma": "wp"
}
},
"rules": {
"array-bracket-spacing": [
"error",
"always"
],
"brace-style": [
"error",
"1tbs"
],
"camelcase": [
"error",
{
"properties": "never"
}
],
"comma-dangle": [
"error",
"always-multiline"
],
"comma-spacing": "error",
"comma-style": "error",
"computed-property-spacing": [
"error",
"always"
],
"constructor-super": "error",
"dot-notation": "error",
"eol-last": "error",
"eqeqeq": "error",
"func-call-spacing": "error",
"indent": [
"error",
"tab",
{
"SwitchCase": 1
}
],
"jsx-a11y/label-has-for": [
"error",
{
"required": "id"
}
],
"jsx-a11y/media-has-caption": "off",
"jsx-a11y/no-noninteractive-tabindex": "off",
"jsx-a11y/role-has-required-aria-props": "off",
"jsx-quotes": "error",
"key-spacing": "error",
"keyword-spacing": "error",
"lines-around-comment": "off",
"no-alert": "error",
"no-bitwise": "error",
"no-caller": "error",
"no-console": "error",
"no-const-assign": "error",
"no-debugger": "error",
"no-dupe-args": "error",
"no-dupe-class-members": "error",
"no-dupe-keys": "error",
"no-duplicate-case": "error",
"no-duplicate-imports": "error",
"no-else-return": "error",
"no-eval": "error",
"no-extra-semi": "error",
"no-fallthrough": "error",
"no-lonely-if": "error",
"no-mixed-operators": "error",
"no-mixed-spaces-and-tabs": "error",
"no-multiple-empty-lines": [
"error",
{
"max": 1
}
],
"no-multi-spaces": "error",
"no-multi-str": "off",
"no-negated-in-lhs": "error",
"no-nested-ternary": "error",
"no-redeclare": "error",
"no-restricted-syntax": [
"error",
{
"selector": "ImportDeclaration[source.value=/^@wordpress\\u002F.+\\u002F/]",
"message": "Path access on WordPress dependencies is not allowed."
},
{
"selector": "ImportDeclaration[source.value=/^blocks$/]",
"message": "Use @wordpress/blocks as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^components$/]",
"message": "Use @wordpress/components as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^date$/]",
"message": "Use @wordpress/date as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^editor$/]",
"message": "Use @wordpress/editor as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^element$/]",
"message": "Use @wordpress/element as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^i18n$/]",
"message": "Use @wordpress/i18n as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^data$/]",
"message": "Use @wordpress/data as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^utils$/]",
"message": "Use @wordpress/utils as import path instead."
},
{
"selector": "ImportDeclaration[source.value=/^edit-poost$/]",
"message": "Use @wordpress/edit-post as import path instead."
},
{
"selector": "CallExpression[callee.name=/^__|_n|_x$/]:not([arguments.0.type=/^Literal|BinaryExpression$/])",
"message": "Translate function arguments must be string literals."
},
{
"selector": "CallExpression[callee.name=/^_n|_x$/]:not([arguments.1.type=/^Literal|BinaryExpression$/])",
"message": "Translate function arguments must be string literals."
},
{
"selector": "CallExpression[callee.name=_nx]:not([arguments.2.type=/^Literal|BinaryExpression$/])",
"message": "Translate function arguments must be string literals."
}
],
"no-shadow": "error",
"no-undef": "error",
"no-undef-init": "error",
"no-unreachable": "error",
"no-unsafe-negation": "error",
"no-unused-expressions": "error",
"no-unused-vars": "error",
"no-useless-computed-key": "error",
"no-useless-constructor": "error",
"no-useless-return": "error",
"no-var": "error",
"no-whitespace-before-property": "error",
"object-curly-spacing": [
"error",
"always"
],
"padded-blocks": [
"error",
"never"
],
"prefer-const": "error",
"quote-props": [
"error",
"as-needed"
],
"react/display-name": "off",
"react/jsx-curly-spacing": [
"error",
{
"when": "always",
"children": true
}
],
"react/jsx-equals-spacing": "error",
"react/jsx-indent": [
"error",
"tab"
],
"react/jsx-indent-props": [
"error",
"tab"
],
"react/jsx-key": "error",
"react/jsx-tag-spacing": "error",
"react/no-children-prop": "off",
"react/no-find-dom-node": "warn",
"react/prop-types": "off",
"semi": "error",
"semi-spacing": "error",
"space-before-blocks": [
"error",
"always"
],
"space-before-function-paren": [
"error",
"never"
],
"space-in-parens": [
"error",
"always"
],
"space-infix-ops": [
"error",
{
"int32Hint": false
}
],
"space-unary-ops": [
"error",
{
"overrides": {
"!": true
}
}
],
"template-curly-spacing": [
"error",
"always"
],
"valid-jsdoc": [
"error",
{
"requireReturn": false
}
],
"valid-typeof": "error",
"yoda": "off"
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Thumbs.db
sftp-config.json
*.sublime-project
*.sublime-workspace
.editorconfig
.editorconfig
.cache/
2 changes: 1 addition & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"preset": "wordpress",
"excludeFiles": [
"**/*.min.js",
"**/*.jsx",
"**/node_modules/**",
"**/build/**",
"**/vendor/**"
]
}
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/build/**
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-exact = true
Binary file added admin/images/amp-story-fallback-poster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added admin/images/amp-story-video-fallback-poster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions admin/js/rt-transcoder-block-editor-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import apiFetch from '@wordpress/api-fetch';

const { rtTranscoderBlockEditorSupport } = window;

const updateAMPStoryPoster = ( BlockEdit ) => {
return ( props ) => {
const mediaAttributes = props.attributes;
const isAMPStory = 'amp/amp-story-page' === props.name;
const isVideoBlock = 'core/video' === props.name;
const mediaId = isAMPStory ? mediaAttributes.mediaId : mediaAttributes.id;
if ( typeof mediaId !== 'undefined' ) {
if ( typeof mediaAttributes.poster === 'undefined' ) {
if ( isAMPStory && typeof mediaAttributes.mediaType !== 'undefined' &&
'video' === mediaAttributes.mediaType && ! mediaAttributes.mediaUrl.endsWith( 'mp4' ) ) {
props.attributes.poster = rtTranscoderBlockEditorSupport.amp_story_fallback_poster;
} else if ( isVideoBlock && typeof mediaAttributes.src !== 'undefined' &&
mediaAttributes.src.indexOf( 'blob:' ) !== 0 && ! mediaAttributes.src.endsWith( 'mp4' ) ) {
props.attributes.poster = rtTranscoderBlockEditorSupport.amp_video_fallback_poster;
}
} else if ( mediaAttributes.poster.endsWith( '-fallback-poster.png' ) ) {
const restBase = '/wp-json/transcoder/v1/amp-media';
apiFetch( {
path: `${ restBase }/${ mediaId }`,
} ).then( data => {
if ( false !== data && null !== data ) {
if ( data.poster.length ) {
if ( isAMPStory && typeof mediaAttributes.mediaType !== 'undefined' && 'video' === mediaAttributes.mediaType ) {
props.attributes.poster = data.poster;
} else if ( isVideoBlock ) {
props.attributes.poster = data.poster;
}
}
}
} );
}
}

return (
<BlockEdit { ...props } />
);
};
};

wp.hooks.addFilter( 'editor.BlockEdit', 'rt-transcoder-amp/with-inspector-controls', updateAMPStoryPoster );
Loading