mirror of
https://github.com/Viglino/ol-ext.git
synced 2026-01-25 17:36:21 +00:00
376 lines
11 KiB
HTML
376 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<!--
|
|
Copyright (c) 2015-2018 Jean-Marc VIGLINO,
|
|
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
|
|
-->
|
|
<title>ol-ext: Undo/redo</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<meta name="description" content="ol.interaction.UndoRedo is an interaction to handle undo on a map." />
|
|
<meta name="keywords" content="ol, openlayers, undo, interaction" />
|
|
|
|
<link rel="stylesheet" href="../style.css" />
|
|
|
|
<!-- jQuery -->
|
|
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
|
|
<!-- FontAwesome -->
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
|
|
|
|
<!-- Openlayers -->
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
|
|
|
|
|
|
<!-- ol-ext -->
|
|
<link rel="stylesheet" href="../../dist/ol-ext.css" />
|
|
<script type="text/javascript" src="../../dist/ol-ext.js"></script>
|
|
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
|
|
<script src="https://unpkg.com/elm-pep"></script>
|
|
|
|
<style>
|
|
.ol-button i {
|
|
color: inherit;
|
|
}
|
|
.ol-button.red button,
|
|
.ol-button.green button,
|
|
.ol-button.blue button {
|
|
color: #f00;
|
|
background-color: currentColor;
|
|
border: 2px solid currentColor;
|
|
width: 1em;
|
|
height: 1em;
|
|
border-radius: 0;
|
|
}
|
|
.ol-button.green button {
|
|
color: #0f0;
|
|
}
|
|
.ol-button.blue button {
|
|
color: #00f;
|
|
}
|
|
.ol-button.red button:hover,
|
|
.ol-button.green button:hover,
|
|
.ol-button.blue button:hover {
|
|
background-color: currentColor!important;
|
|
border-color: #000;
|
|
}
|
|
.options ul {
|
|
cursor: pointer;
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
.options li {
|
|
padding: .1em .5em;
|
|
}
|
|
.options li:hover {
|
|
background-color: #369;
|
|
color: #fff;
|
|
}
|
|
.options .redo li {
|
|
opacity: .4;
|
|
}
|
|
.options button {
|
|
margin: auto;
|
|
display: block;
|
|
}
|
|
.options li:before {
|
|
font-family: FontAwesome;
|
|
display: inline-block;
|
|
width: 1.4em;
|
|
height: 1.4em;
|
|
border-radius: .3em;
|
|
background-color: #369;
|
|
color: #fff;
|
|
padding: .2em;
|
|
text-align: center;
|
|
box-sizing: border-box;
|
|
margin-right: .3em;
|
|
}
|
|
.options li.addfeature:before {
|
|
content: "\f067";
|
|
}
|
|
.options li.modify:before {
|
|
content: "\f040";
|
|
}
|
|
.options li.translate:before {
|
|
content: "\f0b2";
|
|
}
|
|
.options li.rotate:before {
|
|
content: "\f01e";
|
|
}
|
|
.options li.scale:before {
|
|
content: "\f047";
|
|
}
|
|
.options li.delete:before {
|
|
content: "\f1f8";
|
|
}
|
|
.options li.split:before {
|
|
content: "\f0c4";
|
|
}
|
|
.options li.color:before {
|
|
content: "\f1fc";
|
|
}
|
|
</style>
|
|
|
|
</head>
|
|
<body >
|
|
<a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>
|
|
|
|
<a href="../../index.html">
|
|
<h1>ol-ext: Undo/redo</h1>
|
|
</a>
|
|
<div class="info">
|
|
<p>
|
|
<i>ol/interaction/UndoRedo</i> is an interaction to handle undo/redo on a map.
|
|
</p>
|
|
<p>
|
|
<span class="experimental">Experimental</span>
|
|
this feature is still in progress and may be buggy.
|
|
</p>
|
|
<p>
|
|
Just add the interaction to the map to get it working.
|
|
It will watch all vector sources and all interactions in the map.
|
|
<ul>
|
|
<li>
|
|
Use the <i>undo()</i> and <i>redo()</i> method to undo the last recorded operations.
|
|
</li><li>
|
|
The <i>shift()</i> method lets you remove an operation from the undo stack.
|
|
</li><li>
|
|
Use the <i>getStack()</i> method to have a list of current undo actions in the stack.
|
|
</li><li>
|
|
<i>change:add</i>, <i>change:remove</i> or <i>change:clear</i> events
|
|
are triggered when the undo stack change.
|
|
</li><li>
|
|
Undo can be stacked in a block to make them undoing at a time.
|
|
Just enclose the operation between a <i>blockStart()</i> and <i>blockEnd()</i>.
|
|
</li><li>
|
|
You can stack operation on a map by enclosing operations beetween
|
|
<i>undoblockstart</i> and <i>undoblockend</i> events
|
|
(<i>map.dispatchEvent('undoblockstart')</i> and <i>map.dispatchEvent('undoblockend')</i>).
|
|
</li><li>
|
|
Use <i>setMaxLength()</i> and <i>setMaxSize()</i> methods to set the max stack length and max heap size.
|
|
</li>
|
|
</ul>
|
|
</p>
|
|
Cancelable operations:
|
|
<ul>
|
|
<li>
|
|
<b>ol/source/Vector</b>
|
|
<i>addFeature</i> / <i>removeFeature</i> / <i>clear</i>
|
|
<br/>
|
|
The core <i>clear</i> function has been overwritten to fires
|
|
a <i>clearstart</i> and <i>clearend</i> event
|
|
</li>
|
|
</ul>
|
|
<br/>
|
|
Cancelable interactions:
|
|
<ul>
|
|
<li>
|
|
<b>ol/interaction/Draw</b> (ol core interaction)
|
|
</li>
|
|
<li>
|
|
<a href="../interaction/map.interaction.modifyfeature.html">
|
|
<b>ol/interaction/ModifyFeature</b>
|
|
</a>
|
|
The core <i>ol/interaction/Modify</i> has been rewritten to handle modification
|
|
start / end events
|
|
(see <a href="../interaction/map.interaction.modifyfeature.html">example</a>)
|
|
</li>
|
|
<li>
|
|
<b>ol/interaction/Delete</b>
|
|
</li>
|
|
<li>
|
|
<a href="../interaction/map.interaction.drawhole.html">
|
|
<b>ol/interaction/DrawHole</b>
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a href="../interaction/map.interaction.transform.html">
|
|
<b>ol/interaction/Transform</b>
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a href="../interaction/map.interaction.split.html">
|
|
<b>ol/interaction/Split</b>
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a href="../interaction/map.interaction.offset.html">
|
|
<b>ol/interaction/Offset</b>
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<b>ol/interaction/FillAttribute</b> (see <a href="./map.interaction.undoredo2.html">example</a>)
|
|
</li>
|
|
</ul>
|
|
<p>
|
|
To add your own cancelable actions, look at
|
|
<a href="./map.interaction.undocustom.html">this example</a>.
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Map div -->
|
|
<div id="map" style="width:600px; height:400px;"></div>
|
|
|
|
<div class="options" >
|
|
<h2>Undo stack</h2>
|
|
<button onclick="undoInteraction.clear()">clear stack...</button>
|
|
<hr/>
|
|
<ul class="undo"></ul>
|
|
<hr/>
|
|
<ul class="redo"></ul>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
// Vector layer
|
|
var vector = new ol.layer.Vector( {
|
|
source: new ol.source.Vector(),
|
|
style: function (f) {
|
|
return new ol.style.Style({
|
|
image: new ol.style.Circle({
|
|
radius: 5,
|
|
stroke: new ol.style.Stroke({ width: 1.5, color: f.get('color') || [255,128,0] }),
|
|
fill: new ol.style.Fill({ color: (f.get('color') || [255,128,0]).concat([.3]) })
|
|
}),
|
|
stroke: new ol.style.Stroke({ width: 2.5, color: f.get('color') || [255,128,0] }),
|
|
fill: new ol.style.Fill({ color: (f.get('color') || [255,128,0]).concat([.3]) })
|
|
})
|
|
}
|
|
})
|
|
|
|
// The map
|
|
var map = new ol.Map ({
|
|
target: 'map',
|
|
view: new ol.View ({
|
|
zoom: 14,
|
|
center: [270701, 6247637]
|
|
}),
|
|
layers: [
|
|
new ol.layer.Tile({ source: new ol.source.OSM() }),
|
|
vector
|
|
]
|
|
});
|
|
|
|
// Main control bar
|
|
var mainbar = new ol.control.Bar();
|
|
map.addControl(mainbar);
|
|
|
|
// Editbar
|
|
var editbar = new ol.control.EditBar ({
|
|
source: vector.getSource(),
|
|
interactions: { Info: false }
|
|
});
|
|
mainbar.addControl(editbar);
|
|
|
|
// Add a fill interaction to set color attribute
|
|
var fill = new ol.interaction.FillAttribute({ name: 'fill color' }, { color: [255,0,0] });
|
|
editbar.addControl(new ol.control.Toggle({
|
|
html: '<i class="fa fa-paint-brush"></i>',
|
|
title: 'fill color attribut',
|
|
interaction: fill,
|
|
bar: new ol.control.Bar({
|
|
controls:[
|
|
new ol.control.Button({
|
|
className: 'red',
|
|
handleClick: function(){
|
|
fill.setAttribute('color', [255,0,0])
|
|
}
|
|
}),
|
|
new ol.control.Button({
|
|
className: 'green',
|
|
handleClick: function(){
|
|
fill.setAttribute('color', [0,255,0])
|
|
}
|
|
}),
|
|
new ol.control.Button({
|
|
className: 'blue',
|
|
handleClick: function(){
|
|
fill.setAttribute('color', [0,0,255])
|
|
}
|
|
})
|
|
]
|
|
})
|
|
}));
|
|
|
|
// Undo redo interaction
|
|
var undoInteraction = new ol.interaction.UndoRedo();
|
|
map.addInteraction(undoInteraction);
|
|
// Prevent selection of a deleted feature
|
|
undoInteraction.on('undo', function(e) {
|
|
if (e.action.type === 'addfeature') {
|
|
editbar.getInteraction('Select').getFeatures().clear();
|
|
editbar.getInteraction('Transform').select();
|
|
}
|
|
});
|
|
|
|
// Handle undo/redo stack
|
|
undoInteraction.on('stack:add', function (e) {
|
|
// New action element
|
|
if (!e.action.element) {
|
|
var elt = e.action.element = $('<li>').text(e.action.name || e.action.type)
|
|
elt.addClass(e.action.name || e.action.type);
|
|
elt.click(function() {
|
|
// undo or redo stack
|
|
if (elt.parent().hasClass('undo')) {
|
|
undoInteraction.undo();
|
|
} else {
|
|
undoInteraction.redo();
|
|
}
|
|
})
|
|
}
|
|
// Append to undo stack
|
|
$('.options .undo').append(e.action.element);
|
|
e.action.element.attr('title', 'undo');
|
|
if (!undoInteraction.hasRedo()) $('.options .redo').html('');
|
|
console.log(undoInteraction.length()+' undo | '+undoInteraction.length('redo')+' redo')
|
|
});
|
|
// Append to redo stack
|
|
undoInteraction.on('stack:remove', function (e) {
|
|
if (e.shift) {
|
|
$('.options .undo li').first().remove();
|
|
} else {
|
|
$('.options .redo').prepend($('.options .undo li').last());
|
|
}
|
|
e.action.element.attr('title', 'redo');
|
|
console.log(undoInteraction.length()+' undo | '+undoInteraction.length('redo')+' redo')
|
|
});
|
|
// Clear stack
|
|
undoInteraction.on('stack:clear', function (e) {
|
|
$('.options .undo').html('');
|
|
$('.options .redo').html('');
|
|
console.log(undoInteraction.length()+' undo | '+undoInteraction.length('redo')+' redo')
|
|
});
|
|
|
|
// Add buttons to the bar
|
|
var bar = new ol.control.Bar({
|
|
group: true,
|
|
controls: [
|
|
new ol.control.Button({
|
|
html: '<i class="fa fa-undo" ></i>',
|
|
title: 'undo...',
|
|
handleClick: function() {
|
|
undoInteraction.undo();
|
|
}
|
|
}),
|
|
new ol.control.Button({
|
|
html: '<i class="fa fa-repeat" ></i>',
|
|
title: 'redo...',
|
|
handleClick: function() {
|
|
undoInteraction.redo();
|
|
}
|
|
})
|
|
]
|
|
});
|
|
mainbar.addControl(bar);
|
|
|
|
// Add a snap
|
|
map.addInteraction(new ol.interaction.Snap({
|
|
source: vector.getSource()
|
|
}));
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html> |