From eb05a95cd13517dee975e5f7a6e1770e075e7403 Mon Sep 17 00:00:00 2001
From: Ben Keith <ben@benlk.com>
Date: Thu, 20 Sep 2018 19:15:37 -0400
Subject: [PATCH 1/2] WIP for https://github.com/INN/pym-shortcode/issues/36,
 not functional

---
 inc/block.php     |  9 +++++++
 inc/shortcode.php | 13 +++++++++-
 js/block.js       | 65 +++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 78 insertions(+), 9 deletions(-)

diff --git a/inc/block.php b/inc/block.php
index fe20aee..659fbe7 100644
--- a/inc/block.php
+++ b/inc/block.php
@@ -49,6 +49,15 @@ function pym_block_init() {
 			),
 			'id' => array(
 				'type' => 'string',
+				'source' => 'attribute',
+				'selector' => 'a',
+				'attribute' => 'id',
+			),
+			'anchor' => array(
+				'type' => 'string',
+				'source' => 'attribute',
+				'selector' => 'a',
+				'attribute' => 'id',
 			),
 			'className' => array(
 				'type' => 'string',
diff --git a/inc/shortcode.php b/inc/shortcode.php
index 0eff0c0..8520f19 100644
--- a/inc/shortcode.php
+++ b/inc/shortcode.php
@@ -32,7 +32,18 @@ function pym_shortcode( $atts = array(), $content = '', $tag = '' ) {
 
 	// Set us up the vars.
 	$pymoptions = empty( $atts['pymoptions'] ) ? '' : $atts['pymoptions'];
-	$id = empty( $atts['id'] ) ? '' : esc_attr( $atts['id'] );
+
+	error_log(var_export( $atts, true));
+	// The element ID passed by the shortcode or the block
+	if ( ! empty( $atts['id'] ) ) {
+		$id = esc_attr( $atts['id'] );
+	} elseif ( ! empty( $atts['anchor'] ) ) {
+		$id = esc_attr( $atts['anchor'] );
+	} else {
+		$id = '';
+	}
+
+	// the parent element's ID;
 	$actual_id = empty( $id ) ? 'pym_' . $pym_id : $id;
 
 	/**
diff --git a/js/block.js b/js/block.js
index c5ce11d..3ee0e60 100755
--- a/js/block.js
+++ b/js/block.js
@@ -71,7 +71,7 @@
 			html: false, // Removes support for an HTML mode.
 			align: true, // supports alignment
 			alignWide: true, // supports the extra slignment
-			anchor: false, // see https://github.com/INN/pym-shortcode/issues/36
+			anchor: true,
 			customClassName: true,
 			className: true,
 			inserter: true,
@@ -104,6 +104,7 @@
 					{
 						// attributes
 						className: props.className,
+						anchor: props.anchor,
 					},
 					// children follow
 					el( TextControl, {
@@ -137,12 +138,6 @@
 					} ),
 				),
 				el( InspectorAdvancedControls, {},
-					el( TextControl, {
-						label: __( 'Parent Element ID (optional)' ),
-						value: props.attributes.id,
-						onChange: ( value ) => { props.setAttributes( { id: value } ); },
-						help: __( 'The Pym.js block will automatically generate an ID for the parent element and use that to initiate the Pym.js embed. If your child page\'s code requires its parent to have a specific element ID, set that here.' ),
-					} ),
 					el( TextControl, {
 						label: __( 'Pym.js Source URL (optional)' ),
 						value: props.attributes.pymsrc,
@@ -177,6 +172,9 @@
 		 * Though this block has a render callback, we save the URL of the embed in the post_content
 		 * just in case this plugin is ever deactivated.
 		 *
+		 * The 'anchor' attribute is saved as the 'id' attribute on the element by hacks.
+		 * @see https://github.com/INN/pym-shortcode/issues/36
+		 *
 		 * @return {Element}       Element to render.
 		 */
 		save: function( props ) {
@@ -184,6 +182,8 @@
 				'a',
 				{
 					href: props.attributes.src,
+					id: props.attributes.anchor,
+					className: props.attributes.class,
 				},
 				props.attributes.src
 			);
@@ -219,7 +219,7 @@
 								return named.pymoptions ? named.pymoptions : '';
 							},
 						},
-						id: {
+						anchor: {
 							type: 'string',
 							shortcode: function( named ) {
 								return named.id ? named.id : '';
@@ -243,6 +243,55 @@
 			]
 			// @todo provide "to" transformations for embed, plain HTML, etc
 		},
+
+		/**
+		 * Deprecated previous versions of this block
+		 *
+		 * - attributes
+		 * - support
+		 * - save
+		 * - migrate function
+		 * - isEligible
+		 *
+		 * @link https://wordpress.org/gutenberg/handbook/block-api/deprecated-blocks/
+		 */
+		deprecated: [
+			{
+				// before https://github.com/INN/pym-shortcode/issues/36
+				save: function( props ) {
+					return wp.element.createElement(
+						'a',
+						{
+							href: props.attributes.src,
+						},
+						props.attributes.src
+					);
+				},
+				migrate: function( attributes, innerBlocks ) {
+					attributes.anchor = attributes.id;
+					return [ attributes, innerBlocks ];
+				},
+			},
+			{
+				// before https://github.com/INN/pym-shortcode/issues/36
+				save: function( props ) {
+					return wp.element.createElement(
+						'a',
+						{
+							href: props.attributes.src,
+							anchor: props.attributes.anchor,
+							id: props.attributes.anchor,
+							className: props.attributes.class,
+						},
+						props.attributes.src
+					);
+				},
+				migrate: function( attributes, innerBlocks ) {
+					attributes.anchor = attributes.id;
+					return [ attributes, innerBlocks ];
+				},
+			},
+		]
 	} );
 } )(
 	window.wp

From 5e864e350da0b50a938f2b7c9f7d9f18cf5c6f6f Mon Sep 17 00:00:00 2001
From: Ben Keith <ben@benlk.com>
Date: Thu, 20 Sep 2018 20:13:36 -0400
Subject: [PATCH 2/2] Make selectors * for attributes attempting to pull the
 anchor attribute from the block

---
 inc/block.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/inc/block.php b/inc/block.php
index 659fbe7..b7cf223 100644
--- a/inc/block.php
+++ b/inc/block.php
@@ -50,14 +50,14 @@ function pym_block_init() {
 			'id' => array(
 				'type' => 'string',
 				'source' => 'attribute',
-				'selector' => 'a',
 				'attribute' => 'id',
+				'selector' => '*',
 			),
 			'anchor' => array(
 				'type' => 'string',
 				'source' => 'attribute',
-				'selector' => 'a',
 				'attribute' => 'id',
+				'selector' => '*',
 			),
 			'className' => array(
 				'type' => 'string',