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

Commit

Permalink
undo
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed May 31, 2014
1 parent fb0f2f9 commit a348f0d
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 18 deletions.
62 changes: 48 additions & 14 deletions elements/designer-element/designer-element.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<link rel="import" href="../x-palette/x-palette.html">
<link rel="import" href="../x-tree/x-tree.html">
<link rel="import" href="../x-preview/x-github-info.html">
<link rel="import" href="../x-file-document/x-file-document.html">
<link rel="import" href="../x-state/x-state.html">

<polymer-element name="designer-element" vertical layout>
<template>
Expand All @@ -25,15 +25,19 @@

<core-toolbar id="appbar" class="narrow">

<core-icon-button icon="unfold-more" class="rotate {{ {selected: selected === 'code'} | tokenList }}" name="code" on-tap="{{codeToggleAction}}"></core-icon-button>
<core-icon-button icon="save" on-tap="{{saveAction}}"></core-icon-button>
<core-icon-button title="Toggle Code/Design" icon="unfold-more" class="rotate {{ {selected: selected === 'code'} | tokenList }}" name="code" on-tap="{{codeToggleAction}}"></core-icon-button>
<core-icon-button title="Save" icon="save" on-tap="{{saveAction}}"></core-icon-button>
<!-- shareAction must be triggered from click to allow opening a tab (otherwise can only open a floating window) -->
<core-icon-button icon="social:share" on-click="{{shareAction}}"></core-icon-button>
<core-icon-button icon="launch" on-click="{{previewAction}}"></core-icon-button>
<core-icon-button title="Share Gist" icon="social:share" on-click="{{shareAction}}"></core-icon-button>
<core-icon-button title="Launch Preview" icon="launch" on-click="{{previewAction}}"></core-icon-button>

<span style="width: 16px;"></span>
<core-icon-button title="Undo" disabled?="{{!$.state.canBack}}" icon="rotate-left" on-tap="{{undoAction}}"></core-icon-button>
<core-icon-button title="Redo" disabled?="{{!$.state.canForward}}" icon="rotate-right" on-tap="{{redoAction}}"></core-icon-button>

<div flex></div>

<core-icon-button icon="{{maximized ? 'fullscreen-exit' : 'fullscreen'}}" hidden?="{{selected == 'code'}}" on-tap="{{fullscreenAction}}"></core-icon-button>
<core-icon-button title="Toggle Fullwidth" icon="{{maximized ? 'fullscreen-exit' : 'fullscreen'}}" hidden?="{{selected == 'code'}}" on-tap="{{fullscreenAction}}"></core-icon-button>

</core-toolbar>

Expand All @@ -54,11 +58,11 @@
<div class="inspectorControls" flex vertical layout>
<core-toolbar class="designTools narrow">
<div id="selectedElement" flex>
<core-icon-button icon="chevron-left" on-click="{{selectParentElement}}" hidden?="{{!selectedName}}"></core-icon-button>
<core-icon-button title="Select Parent" icon="chevron-left" on-click="{{selectParentElement}}" hidden?="{{!selectedName}}"></core-icon-button>
{{selectedName}}
</div>

<core-menu-button icon="more-vert" halign="right">
<core-menu-button title="More..." icon="more-vert" halign="right">
<style>
.core-selected {
font-weight: normal;
Expand All @@ -74,8 +78,8 @@
<core-item icon="arrow-forward" on-tap="{{demoteElement}}">Move Forward</core-item>
<core-item icon="fullscreen" on-tap="{{maximizeElement}}">Auto-size</core-item>
</core-menu-button>
<core-icon-button icon="delete" on-click="{{deleteElement}}"></core-icon-button>
<core-icon-button icon="{{maximized ? 'fullscreen-exit' : 'fullscreen'}}" on-tap="{{fullscreenAction}}"></core-icon-button>
<core-icon-button title="Delete" icon="delete" on-click="{{deleteElement}}"></core-icon-button>
<core-icon-button title="Toggle Fullwidth" icon="{{maximized ? 'fullscreen-exit' : 'fullscreen'}}" on-tap="{{fullscreenAction}}"></core-icon-button>
</core-toolbar>
<x-inspector flex id="inspector" on-bind-property="{{applyPropertyBinding}}"></x-inspector>
</div>
Expand Down Expand Up @@ -104,6 +108,7 @@
</core-pages>

<x-dom-serializer id="serializer"></x-dom-serializer>
<x-state id="state" on-state-change="{{undoStateChange}}"></x-state>
<x-github-info id="githubInfo" token="{{githubToken}}"></x-github-info>
<github-element id="github" token="{{githubToken}}" on-files-loaded="{{documentLoaded}}" on-files-saved="{{documentSaved}}" on-files-updated="{{documentSaved}}" on-token-changed="{{tokenChangeHandler}}"></github-element>

Expand Down Expand Up @@ -169,6 +174,7 @@
this.$.inspector.sourceElement = this.designer.selected;
this.$.tree.selected = this.designer.selected;
this.selectedName = this.makeSelectedName(this.designer.selected);
this.recordChange();
},

makeSelectedName: function(element) {
Expand Down Expand Up @@ -199,7 +205,7 @@
paletteDrag: function(event, dragInfo) {
// allow dragging over iframe
var frameStyle = this.$.frame.style;
frameStyle.pointerEvents = 'none'
frameStyle.pointerEvents = 'none';
// adjust event frame of reference
var rect = this._frameRect = this._frameRect ||
this.$.frame.getBoundingClientRect();
Expand Down Expand Up @@ -309,9 +315,6 @@
},

codeToDesign: function() {
//this.$.frame.style.display = 'none';
//this.pendingHtml = this.code;
//this.reloadDesigner();
this.loadHtml(this.code);
},

Expand Down Expand Up @@ -432,6 +435,37 @@
this._signInCallback.call(this);
this._signInCallback = null;
}
},

undoAction: function() {
this.$.state.back();
},

redoAction: function() {
this.$.state.forward();
},

undoStateChange: function(e, detail) {
console.log('undoStateChange')
this._preventStateChange = true;
this.loadHtml(detail.state);
},

recordChange: function() {
if (!this._preventStateChange) {
this.job('record-state', function() {
var n = this.html;
if (this._lastHtml !== n) {
this._lastHtml = n;
//console.log('recordChange, push');
this.$.state.push(n);
}
}, 100);
} else {
this.async(function() {
this._preventStateChange = false;
});
}
}

});
Expand Down
8 changes: 4 additions & 4 deletions elements/x-property-inspector/x-property-inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
:host {
display: block;
white-space: nowrap;
margin-bottom: 1rem;
margin-bottom: 0.5rem;
}
.property-name {
text-overflow: ellipsis;
overflow: hidden;
color: #919191;
font-weight: bold;
margin-bottom: 1em;
/*font-weight: bold;*/
margin-bottom: 0.5em;
}
#container {
display: inline-block;
Expand Down Expand Up @@ -50,7 +50,7 @@
display: none !important;
}
</style>
<div class="property-name">{{property.name}}:</div>
<div class="property-name">{{property.meta.label || property.name}}</div>
<span id="container"><content></content></span>
<div id="iconContainer" on-click="{{linkTap}}" nodisplay?="{{nobind}}">
<core-icon id="icon" icon="link" size="16"></core-icon>
Expand Down
50 changes: 50 additions & 0 deletions elements/x-state/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html>
<head>
<title>x-state</title>

<script src="components/platform/platform.js"></script>
<link rel="import" href="components/polymer/polymer.html">
<link rel="import" href="x-state.html">
</head>
<body>

<template is="auto-binding">
<button on-tap="{{push}}">Push</button>
<button on-tap="{{back}}" disabled?="{{!$.state.canBack}}">Back</button>
<button on-tap="{{forward}}" disabled?="{{!$.state.canForward}}">Forward</button>
<x-state id="state" on-last-state="{{last}}" on-first-state="{{first}}" on-state-change="{{change}}"></x-state>
<div>{{state}}</div>
</template>
<script>
var scope = document.querySelector('template');
scope.count = 0;
scope.push = function() {
this.count++;
this.updateState();
this.$.state.push(scope.state);
}

scope.back = function() {
this.$.state.back();
}

scope.forward = function() {
this.$.state.forward();
}

scope.change = function(e, detail) {
this.state = detail.state;
}

scope.updateState = function() {
return this.state = (this.count + Math.random()).toFixed(3);
}

addEventListener('template-bound', function(e) {
scope.$.state.push(scope.updateState());
})
</script>

</body>
</html>
72 changes: 72 additions & 0 deletions elements/x-state/x-state.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

<polymer-element name="x-state" attributes="canBack canForward">
<script>

Polymer('x-state', {
_cursor: -1,
cursor: -1,
canBack: false,
canForward: false,
created: function() {
this.stack = [];
},

push: function(state) {
if (!state && state !== 0) {
return;
}
if (!this.isAtLast()) {
var f = this.cursor + 1;
this.stack.splice(f, this.stack.length - f);
}
this.stack.push(state);
this.cursor = this.stack.length - 1;
this._statePushed = true;
Platform.flush();
},

back: function() {
this._cursor--;
},

forward: function() {
this._cursor++;
},

_cursorChanged: function() {
this.cursor = Math.max(0, Math.min(this._cursor,
this.stack.length-1));
},

cursorChanged: function(old) {
//console.log('cursorChanged', this.cursor, this._statePushed);
this._cursor = this.cursor;
if (!this._statePushed) {
var state = this.stack[this.cursor];
this.fire('state-change', {state: state});
} else {
this._statePushed = false;
}
var first = this.isAtFirst(), last = this.isAtLast();
this.canBack = !first;
this.canForward = !last;
if (first) {
this.fire('first-state');
} else if (last) {
this.fire('last-state');
}
},

isAtLast: function() {
return (this.cursor === this.stack.length -1)
},

isAtFirst: function() {
return (this.cursor === 0)
}

});

</script>

</polymer-element>

0 comments on commit a348f0d

Please sign in to comment.