Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Copy & Paste feature #44

Merged
merged 7 commits into from
Jul 2, 2018
Merged

Copy & Paste feature #44

merged 7 commits into from
Jul 2, 2018

Conversation

leekelleher
Copy link
Contributor

Following on from merging PR #27 in, I've done some refactoring and refinements.

I've not moved anything over to the InnerContent Service API yet, I wanted us to be happy with how this is progressing. (I've marked the API points with TODO comments)

- Replaced the `window.localStorage` calls with the `localStorageService` (which is used by Umbraco core and has built-in fallbacks)
- Replaced emptying the "key" and removing the "$$hashKey" properties to the `stringify` function
- Reworked the `canPaste` function, as it was being evaluated several times, when the value could be referenced in a local field variable
- Reduced the "Is Content Validate" function, it was over-working
- Set the dirty flag, after pasting content
- Added (temporary) TODO notes
@leekelleher leekelleher added this to the 2.0.0 milestone Apr 23, 2018
@leekelleher leekelleher self-assigned this Apr 23, 2018
Copy link

@mattbrailsford mattbrailsford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick skim over. Just a few comments.

$scope.canAdd = function () {
return (!$scope.model.config.maxItems || $scope.model.config.maxItems === 0 || $scope.model.value.length < $scope.model.config.maxItems) && $scope.model.config.singleItemMode !== "1";
return (!$scope.model.config.maxItems || $scope.model.config.maxItems === "0" || $scope.model.value.length < $scope.model.config.maxItems) && $scope.model.config.singleItemMode !== "1";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always going to be string? Should we intParse then comapre?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that prevalues will be strings. This wasn't an issue previously, as we were using double-equals, so the JS was evaluating it. I'd tried to follow the ESLint rules by using the triple-equals, but the value was a string (based on my tests).


$scope.canPaste = function () {
var stackedContentItem = JSON.parse(window.localStorage.getItem("StackedContentCopy"));
if (stackedContentItem && validateModel(stackedContentItem)) return true;
if ($scope.canCopy() && $scope.canAdd()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe canAdd should include a ls.isSupported check so you only need to check canAdd? Seems odd to check canCopy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was that you can't paste without copying it first. But yeah, we can replace it with isSupported.

return;
$scope.copyContent = function (evt, idx) {
// TODO: Move this to InnerContent Service
var item = $scope.model.value[idx];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need an ls.isSupperted check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd thought about it, but the copy feature wouldn't be available if canCopy was false.
Unless if a dev were to hack the API themselves?

}
return v;
}));
allowPaste = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced the allowPaste local variable is fool proof. Is there as possibility this could be false/true but there is something pastable int he clipboard? How expensive is the pastedAllowed() call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It felt pretty expensive. I'd added a console.log in canPaste - I had 5 StackedContent fields on a page, (with at least 10 blocks between them) - meaning it was being called for each field, then each row.

It felt overkill to get the data from localStorage, parse it and loop over the contentType GUIDs each time.

There's mostly a clever way to handle this?

@@ -4,9 +4,10 @@

<div ng-show="inited">

<a ng-if="!model.value || model.value.length == 0" ng-click="addContent($event, 0)" class="placeholder" title="Add content">
<div ng-if="!model.value || model.value.length == 0"ng-click="addContent($event, 0)" class="placeholder" title="Add content">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is wrong here. This should be two links side by side, but it appears the paste link is inside the add link. Needs refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd played around with various options - tried 2 <a> side-by-side, we'd lose the functionality to click anywhere inside the "Add content" panel - users would have to click the "icon-umb-contour" icon.

as localStorageService handles this (and angular.toJson removes the "$$haskKey" property)
@markadrake
Copy link

Recently tested the feature branch for this and in my environment when you click the copy button, it causes the rendered preview to revert back to a simple icon and name template. This is most likely because you are copying the content by reference instead of just value.

	var item = $scope.model.value[idx];

The code is a little goofy, but to avoid copying an object by reference you can stringify and then parse it.

	var item = JSON.parse(JSON.stringify($scope.model.value[idx]));

Doing this resolved the issue. My preview stays in tact.

Otherwise, can you just re-render the preview? But it seems like that's extraneous. Please let me know if you can't reproduce.

@leekelleher
Copy link
Contributor Author

@markadrake - I've only been testing with previews disabled, so I hadn't noticed this, but thank you for spotting it - I'll take a look.

@leekelleher
Copy link
Contributor Author

@markadrake - cool, updated in commit: faf4aff - thanks again!

@leekelleher
Copy link
Contributor Author

Merging into the "dev-v2" branch. I've been using it for several weeks on client projects, it's pretty solid.

@leekelleher leekelleher merged commit d2fed05 into dev-v2 Jul 2, 2018
@leekelleher leekelleher deleted the feature/copy-paste branch July 2, 2018 13:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

3 participants