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

SITES-28486 - open inline editing of a contentfragment in the new cf editor if a feature toggle is set #2916

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -42,6 +44,7 @@
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.adobe.cq.export.json.ComponentExporter;
import com.day.cq.commons.Externalizer;
import com.day.cq.wcm.api.TemplatedResource;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
Expand All @@ -57,6 +60,11 @@ public class ContentFragmentUtils {

private static final Logger LOG = LoggerFactory.getLogger(ContentFragmentUtils.class);

// constants to build the new editor URL
private static final String UNIFIED_SHELL_STAGE_ENDPOINT = "https://experience-stage.adobe.com/";
private static final String UNIFIED_SHELL_PROD_ENDPOINT = "https://experience.adobe.com/";
private static final String AEM_STAGE_ENV = "cmstg";

/**
* The default grid type.
*/
Expand Down Expand Up @@ -321,4 +329,30 @@ public static String getElementMetaType(ContentElement element) {
}
return null;
}

public static String getNewEditorUrl(ResourceResolver resourceResolver, String fragmentPath) {
String newEditorUrl = "";

Externalizer externalizer = resourceResolver.adaptTo(Externalizer.class);
if (externalizer != null) {
String aemAuthorExternalLink = externalizer.externalLink(resourceResolver, Externalizer.AUTHOR, "/");
try {
URL aemInstanceUrl = new URL(aemAuthorExternalLink);
String aemInstanceHost = aemInstanceUrl.getHost();

if (aemInstanceHost != null) {
if (aemInstanceHost.contains(AEM_STAGE_ENV)) {
newEditorUrl = UNIFIED_SHELL_STAGE_ENDPOINT;
} else {
newEditorUrl = UNIFIED_SHELL_PROD_ENDPOINT;
}

newEditorUrl += "?repo=" + aemInstanceUrl.getHost() + "#/aem/cf/editor" + Text.escapePath(fragmentPath);
}
} catch (MalformedURLException e) {
LOG.error("Cannot create URL for the new Content Fragment Editor, malformed external link: {}", aemAuthorExternalLink);
}
}
return newEditorUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ protected ComponentData getComponentData() {
.build();
}

@Override
@NotNull
public String getNewEditorUrl() {
return ContentFragmentUtils.getNewEditorUrl(resourceResolver, fragmentPath);
}

/**
* Empty placeholder content fragment.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,10 @@ default String getExportedType() {
default String getEditorJSON() {
return "";
}

@NotNull
@JsonIgnore
default String getNewEditorUrl() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@Version("1.5.1")
@Version("1.6.0")
package com.adobe.cq.wcm.core.components.models.contentfragment;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.wcm.core.components.context.CoreComponentTestContext;
import com.day.cq.commons.Externalizer;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import io.wcm.testing.mock.aem.junit5.AemContext;
Expand Down Expand Up @@ -327,6 +328,24 @@ public void testGetElementMetaType() {
assertEquals("testMetaType", metaType);
}

@Test
public void testGetNewEditorUrl() {
ResourceResolver resourceResolver = Mockito.mock(ResourceResolver.class);
Externalizer externalizer = Mockito.mock(Externalizer.class);
Mockito.when(resourceResolver.adaptTo(Externalizer.class)).thenReturn(externalizer);
String fragmentPath = "/content/dam/cf1";

Mockito.when(externalizer.externalLink(resourceResolver, Externalizer.AUTHOR, "/"))
.thenReturn("https://author-p12345-e1234.adobeaemcloud.com");
assertEquals("https://experience.adobe.com/?repo=author-p12345-e1234.adobeaemcloud.com#/aem/cf/editor/content/dam/cf1",
ContentFragmentUtils.getNewEditorUrl(resourceResolver, fragmentPath));

Mockito.when(externalizer.externalLink(resourceResolver, Externalizer.AUTHOR, "/"))
.thenReturn("https://author-p12345-e1234-cmstg.adobeaemcloud.com");
assertEquals("https://experience-stage.adobe.com/?repo=author-p12345-e1234-cmstg.adobeaemcloud.com#/aem/cf/editor/content/dam/cf1",
ContentFragmentUtils.getNewEditorUrl(resourceResolver, fragmentPath));
}

/**
* Dummy test {@link ComponentExporter component exporter}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
var CLASS_CONTENTFRAGMENT = "cmp-contentfragment";
// name of the attribute on the content fragment storing its path
var ATTRIBUTE_PATH = "data-cmp-contentfragment-path";
// name of the attribute on the content fragment storing the new editor url
var ATTRIBUTE_NEW_EDITOR_URL = "data-cmp-contentfragment-neweditorurl";
// base URL of the editor
var EDITOR_URL = "/editor.html";

Expand All @@ -37,13 +39,21 @@
// get the path of the content fragment
var fragmentPath = $(editable.dom).find("." + CLASS_CONTENTFRAGMENT).attr(ATTRIBUTE_PATH);
if (fragmentPath) {
var fragmentEditUrl = EDITOR_URL + fragmentPath;
var fragment = ns.CFM.Fragments.adaptToFragment(editable.dom);
if (fragment && typeof fragment.variation !== "undefined" && fragment.variation !== "master") {
fragmentEditUrl = fragmentEditUrl + "?variation=" + fragment.variation;
var editorUrl = "";
// check if the url for new editor is set
if (Granite.Toggles && Granite.Toggles.isEnabled("FT_SITES-19326")) {
editorUrl = $(editable.dom).find("." + CLASS_CONTENTFRAGMENT).attr(ATTRIBUTE_NEW_EDITOR_URL);
}
// if the url for the new editor is set build the URL to the old one
if (!editorUrl) {
editorUrl = EDITOR_URL + fragmentPath;
var fragment = ns.CFM.Fragments.adaptToFragment(editable.dom);
if (fragment && typeof fragment.variation !== "undefined" && fragment.variation !== "master") {
editorUrl = editorUrl + "?variation=" + fragment.variation;
}
}
// open the editor in a new window
window.open(Granite.HTTP.externalize(fragmentEditUrl));
window.open(Granite.HTTP.externalize(editorUrl));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class="cmp-contentfragment cmp-contentfragment--${fragment.name}"
data-cmp-contentfragment-model="${fragment.type}"
data-sly-attribute.data-cmp-contentfragment-path="${fragmentPath}"
data-sly-attribute.data-cmp-contentfragment-neweditorurl="${fragment.newEditorUrl}"
data-json="${!wcmmode.disabled && fragment.editorJSON}"
data-cmp-data-layer="${fragment.data.json}">
<h3 class="cmp-contentfragment__title">${fragment.title}</h3>
Expand Down
Loading