Skip to content

Commit

Permalink
Opacities action (publiclab#1260)
Browse files Browse the repository at this point in the history
* bug _cornerExceedsMapLats, set positive values

* inici OpacitiesAction

* actualitzar projecte

* afegir carpeta node_modules

* intentar carregar llibreries

* carregar leaflet desde CDN

* afegir icona Opacities

* posar icona opacities a la barra

* afegir icona opacities, correcció de bugs i rebuild

* canvi icona Opacities

* probar el sub-menu

* sub-menu test 1

* sub-menu test 2

* sub-menu test 3

* sub-menu test 4

* sub-menu test 5

* sub-menu test 6

* sub-menu test 7

* sub-menu test 8

* sub-menu test 9

* sub-menu test 10

* sub-menu test 11

* sub-menu test 12

* sub-menu test 13

* sub-menu test 14

* sub-menu test 15

* sub-menu test 16

* sub-menu test 17

* sub-menu test 18

* sub-menu test 19

* sub-menu test 20

* sub-menu test 21

* sub-menu test 22

* sub-menu test 23

* sub-menu test 24

* sub-menu test 25

* sub-menu test 26

* sub-menu test 26

* sub-menu test 27

* sub-menu test 28

* sub-menu test 29

* sub-menu test 30

* sub-menu test 31

* sub-menu test 32

* sub-menu test 33

* sub-menu test 34

* sub-menu test 35

* sub-menu test 36

* sub-menu test 37

* sub-menu final test, pending a few know issues

* correct Opacities icon, incorrect change when OpacityAction is fired

* add comment in OpacitiesAction

* correct undesired values in opacities array

* millorar aplicació dels style al sub-toolbar

* ajust css text sub-toolbar

* ajust css text sub-toolbar

* bug event propagation increase sub-toolbar z-index

* re-build after merge for Opacities activation

* bug barra colorize mal activada

* cleaning

* rebuild

* afegir icones

* The Opacities action has been moved up, above the toolbar

* correct some mistakes

* correct vertical displacement

* re-build

* incliude CSS overwrites

* correct _opacity assignement after opacities change

* mini bug

* correct custom CSSStyleSheet

* run build

* rebuild

* Remove CDN in examples

* final

* update & compile

---------

Co-authored-by: themacboy <[email protected]>
Co-authored-by: Jeffrey Warren <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2023
1 parent 8cca752 commit 9472b3f
Show file tree
Hide file tree
Showing 18 changed files with 611 additions and 8,420 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ package-lock.json
dist/*
!dist/leaflet.distortableimage.css
!dist/leaflet.distortableimage.js
!dist/vendor.js
!dist/vendor.css
!dist/leaflet.distortableimage.js.map
1 change: 1 addition & 0 deletions assets/icons/svg-min/opacities.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/icons/svg/opacities.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions assets/icons/symbol/sprite.symbol.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
.svg-lock-dims { width: 18px; height: 18px; }
.svg-opacity-dims { width: 18px; height: 18px; }
.svg-opacity_empty-dims { width: 14px; height: 18px; }
.svg-opacities-dims { width: 14px; height: 18px; }
.svg-restore-dims { width: 18px; height: 18px; }
.svg-rotate-dims { width: 18px; height: 18px; }
.svg-scale-dims { width: 18px; height: 18px; }
Expand Down Expand Up @@ -76,6 +77,7 @@
<symbol viewBox="0 0 18 18" id="lock"><path d="M13.5 6h-.75V4.5C12.75 2.43 11.07.75 9 .75 6.93.75 5.25 2.43 5.25 4.5V6H4.5C3.675 6 3 6.675 3 7.5V15c0 .825.675 1.5 1.5 1.5h9c.825 0 1.5-.675 1.5-1.5V7.5c0-.825-.675-1.5-1.5-1.5zM6.75 4.5A2.247 2.247 0 019 2.25a2.247 2.247 0 012.25 2.25V6h-4.5V4.5zM13.5 15h-9V7.5h9V15zM9 12.75c.825 0 1.5-.675 1.5-1.5s-.675-1.5-1.5-1.5-1.5.675-1.5 1.5.675 1.5 1.5 1.5z"/></symbol>
<symbol viewBox="0 0 18 18" id="opacity"><path d="M13.245 6L9 1.763 4.755 6A6.015 6.015 0 003 10.23c0 1.5.585 3.082 1.755 4.252a5.993 5.993 0 008.49 0A6.066 6.066 0 0015 10.23c0-1.5-.585-3.06-1.755-4.23zM4.5 10.5c.008-1.5.465-2.453 1.32-3.3L9 3.952l3.18 3.285c.855.84 1.313 1.763 1.32 3.263h-9z"/></symbol>
<symbol viewBox="0 0 14 18" id="opacity_empty"><path stroke="#0078A8" stroke-width="1.7" d="M10.708 6.25A5.113 5.113 0 0112.2 9.846c0 1.275-.497 2.62-1.492 3.614a5.094 5.094 0 01-7.216 0A5.156 5.156 0 012 9.846c0-1.275.497-2.601 1.492-3.596L7.1 2.648l3.608 3.602zm0 0L7.1 2.648 3.492 6.25A5.113 5.113 0 002 9.846c0 1.275.497 2.62 1.492 3.614a5.094 5.094 0 007.216 0A5.156 5.156 0 0012.2 9.846a5.113 5.113 0 00-1.492-3.596z"/></symbol>
<symbol viewBox="0 0 14 18" id="opacities"><g fill="#0078a8"><path d="m2.1 10.85c.2.98.68 1.89 1.39 2.61 0 0 .01.01.01.01.99.99 2.3 1.49 3.6 1.49h.01c1.3 0 2.6-.5 3.59-1.5.73-.73 1.19-1.66 1.39-2.61z" opacity=".75"/><path d="m2 9.85c0 .34.04.68.1 1.01h9.99c.07-.34.11-.67.11-1.01 0-1.13-.4-2.21-1.08-3.09h-8.04c-.7.91-1.08 2.01-1.08 3.09z" opacity=".5"/><path d="m3.08 6.75h8.03c-.13-.17-.26-.35-.41-.5l-3.6-3.6-3.61 3.6c-.15.16-.27.33-.41.5z" opacity=".25"/></g><path d="m10.71 6.25c.95.96 1.49 2.25 1.49 3.6 0 1.27-.5 2.62-1.49 3.61-1.99 1.99-5.21 2-7.2.01 0 0-.01-.01-.01-.01-.96-.96-1.5-2.26-1.5-3.61 0-1.27.5-2.6 1.49-3.6l3.61-3.6z" fill="none" stroke="#0078a8" stroke-width=".1"/><path d="m10.71 6.25c.95.96 1.49 2.25 1.49 3.6 0 1.27-.5 2.62-1.49 3.61-1.99 1.99-5.21 2-7.2.01 0 0-.01-.01-.01-.01-.96-.96-1.5-2.26-1.5-3.61 0-1.27.5-2.6 1.49-3.6l3.61-3.6z" fill="none" stroke="#0078a8" stroke-width="1.7"/></symbol>
<symbol viewBox="0 0 18 18" id="restore"><path d="M15.67 3.839a.295.295 0 00-.22.103l-5.116 3.249V4.179a.342.342 0 00-.193-.315.29.29 0 00-.338.078L3.806 7.751v-4.63h-.002l.002-.022c0-.277-.204-.502-.456-.502h-.873V2.6c-.253 0-.457.225-.457.503l.002.026v10.883h.005c.021.257.217.454.452.455l.016-.002h.822c.013.001.025.004.038.004.252 0 .457-.225.457-.502a.505.505 0 00-.006-.068V9.318l6.001 3.811a.288.288 0 00.332.074.34.34 0 00.194-.306V9.878l5.12 3.252a.288.288 0 00.332.073.34.34 0 00.194-.306V4.18a.358.358 0 00-.09-.24.296.296 0 00-.218-.1z"/></symbol>
<symbol viewBox="0 0 18 18" id="rotate"><path d="M5.505 4.808L.645 9.675l4.867 4.86 4.868-4.86-4.875-4.868zM2.768 9.675L5.513 6.93 8.25 9.675 5.505 12.42 2.768 9.675zM14.52 4.98A6.713 6.713 0 009.75 3V.57L6.57 3.75l3.18 3.18V4.5a5.23 5.23 0 013.713 1.537 5.255 5.255 0 010 7.426 5.23 5.23 0 01-5.843 1.08L6.503 15.66a6.76 6.76 0 003.247.84c1.725 0 3.457-.66 4.77-1.98a6.735 6.735 0 000-9.54z"/></symbol>
<symbol viewBox="0 0 18 18" id="scale"><path d="M8.25 9h-6a.75.75 0 00-.75.75v6c0 .414.336.75.75.75h6a.75.75 0 00.75-.75v-6A.75.75 0 008.25 9zm-.75 6H3v-4.5h4.5V15zm8.94-13.035a.75.75 0 00-.405-.405.75.75 0 00-.285-.06h-4.5a.75.75 0 000 1.5h2.692L9.967 6.968a.75.75 0 000 1.064.75.75 0 001.065 0L15 4.057V6.75a.75.75 0 101.5 0v-4.5a.75.75 0 00-.06-.285z"/></symbol>
Expand Down Expand Up @@ -275,6 +277,17 @@ <h2>opacity</h2>
</div>
<h2>opacity_empty</h2>
</li>
<li title="opacities">
<div class="icon-box">

<!-- opacities -->
<svg class="svg-opacities-dims">
<use xlink:href="#opacities"></use>
</svg>

</div>
<h2>opacities</h2>
</li>
<li title="restore">
<div class="icon-box">

Expand Down Expand Up @@ -516,6 +529,17 @@ <h2>opacity</h2>
</div>
<h2>opacity_empty</h2>
</li>
<li title="opacities">
<div class="icon-box">

<!-- opacities -->
<svg class="svg-opacities-dims">
<use xlink:href="sprite.symbol.svg#opacities"></use>
</svg>

</div>
<h2>opacities</h2>
</li>
<li title="restore">
<div class="icon-box">

Expand Down
2 changes: 1 addition & 1 deletion assets/icons/symbol/sprite.symbol.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8,418 changes: 2 additions & 8,416 deletions dist/leaflet.distortableimage.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/leaflet.distortableimage.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/vendor.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/vendor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions src/edit/DistortableImage.Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ L.DistortableImage = L.DistortableImage || {};
// holds the keybindings & toolbar API for an individual image instance
L.DistortableImage.Edit = L.Handler.extend({
options: {
opacity: 0.7,
opacity: 0.5,
outline: '1px solid red',
keymap: L.distortableImage.action_map,
},
Expand All @@ -14,6 +14,7 @@ L.DistortableImage.Edit = L.Handler.extend({
this._mode = overlay.options.mode;
this._transparent = false;
this._outlined = false;
this._opacity = options.opacity;

L.setOptions(this, options);

Expand All @@ -32,7 +33,6 @@ L.DistortableImage.Edit = L.Handler.extend({
this._initHandles();
this._appendHandlesandDragable();


if (overlay.isSelected() && !overlay.options.suppressToolbar) {
this._addToolbar();
}
Expand Down Expand Up @@ -359,6 +359,19 @@ L.DistortableImage.Edit = L.Handler.extend({
this._refresh();
},

_setOpacities(o) {
const image = this._overlay.getElement();
this._opacity = o;
if (!this.hasTool(L.OpacitiesAction)) { return; }

(this._opacity < 1) ? this._transparent = true : this._transparent = false;

L.DomUtil.setOpacity(image, this._opacity);
image.setAttribute('opacity', this._opacity);

this._refresh();
},

_toggleBorder() {
const image = this._overlay.getElement();

Expand Down
2 changes: 2 additions & 0 deletions src/edit/actions/EditAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ L.EditAction = L.Toolbar2.Action.extend({
const iconOptions = this.options.toolbarIcon;
const className = iconOptions.className;
const edit = this._overlay.editing;
const style = this.options.toolbarIcon.style;

this.toolbar = toolbar;
this._icon = L.DomUtil.create('li', '', container);
Expand All @@ -40,6 +41,7 @@ L.EditAction = L.Toolbar2.Action.extend({

this._link.setAttribute('href', '#');
this._link.setAttribute('title', iconOptions.tooltip);
if (style) { this._link.setAttribute('style', style); };
this._link.setAttribute('role', 'button');

L.DomUtil.addClass(this._link, this.constructor.baseClass);
Expand Down
133 changes: 133 additions & 0 deletions src/edit/actions/OpacitiesAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
let opacities = [100, 80, 60, 40, 20, 0]; // Set numeric values from 0 to 100.

// Add custom CSS scripts and overwrites. Pending for better implementation of CSSStyleSheet in browsers.
const subtoolbarCss = new CSSStyleSheet();
subtoolbarCss.replaceSync(
`.leaflet-toolbar-icon-vertical {
box-sizing: border-box !important;
display: block !important;
width: 30px !important;
height: 30px !important;
line-height: 30px !important;
padding: 0 !important;
text-align: center !important;
text-decoration: none !important;
background-color: #fff;
border: inset 0.5px lightgray !important;
font-size: 12px !important;
font-weight: bold !important;
color:#0087A8 !important;
float: none !important;
margin: auto !important;
z-index:900 !important;
}
`
);

subtoolbarCss.insertRule(
`.leaflet-toolbar-1 li:first-child a {
border-radius: 4px 4px 0px 0px !important;
}`
);
document.adoptedStyleSheets = [subtoolbarCss];

opacities = opacities.map((o) => {
(isNaN(o) || o > 100) ? o = 100 : o;
(o < 0) ? o = 0 : o;

return L.EditAction.extend({
options: {
toolbarIcon: {
html: o,
tooltip: 'Opacity ' + o +'%',
className: 'leaflet-toolbar-icon-vertical',
style: 'background-color:rgb(' + (100 - o) + '%,' + (100 - o) + '%,' + (100 - o) + '%);',
},
},
addHooks() {
this._overlay.editing._setOpacities(o/100);
},
});
});

L.OpacitiesToolbar2 = L.Toolbar2.extend({
options: {
className: '',
filter: function() { return true; },
actions: [],
style: `translate(-1px, -${ ((opacities.length + 1) * 30)}px)`,
},

appendToContainer(container) {
let baseClass = this.constructor.baseClass + '-' + this._calculateDepth();
let className = baseClass + ' ' + this.options.className;
let Action; let action;
let i; let j; let l; let m;

this._container = container;
this._ul = L.DomUtil.create('ul', className, container);
this._ul.style.transform = ( this.options.style ) ? this.options.style : '';

// Ensure that clicks, drags, etc. don't bubble up to the map.
// These are the map events that the L.Draw.Polyline handler listens for.
// Note that L.Draw.Polyline listens to 'mouseup', not 'mousedown', but
// if only 'mouseup' is silenced, then the map gets stuck in a halfway
// state because it receives a 'mousedown' event and is waiting for the
// corresponding 'mouseup' event.
this._disabledEvents = [
'click', 'mousemove', 'dblclick',
'mousedown', 'mouseup', 'touchstart',
];

for (j = 0, m = this._disabledEvents.length; j < m; j++) {
L.DomEvent.on(this._ul, this._disabledEvents[j], L.DomEvent.stopPropagation);
}

/* Instantiate each toolbar action and add its corresponding toolbar icon. */
for (i = 0, l = this.options.actions.length; i < l; i++) {
Action = this._getActionConstructor(this.options.actions[i]);

action = new Action();
action._createIcon(this, this._ul, this._arguments);
}
},
});


L.OpacitiesAction = L.EditAction.extend({
initialize(map, overlay, options) {
const edit = overlay.editing;
const mode = edit._mode;

options = options || {};
options.toolbarIcon = {
svg: true,
html: 'opacities',
tooltip: 'Set custom opacity',
className: mode === 'lock' ? 'disabled' : '',
};

options.subToolbar = new L.OpacitiesToolbar2({
actions: opacities,
});

L.DistortableImage.action_map.o = mode === 'lock' ? '' : '_setOpacities';

L.EditAction.prototype.initialize.call(this, map, overlay, options);
},

addHooks() {
const link = this._link;
if (L.DomUtil.hasClass(link, 'subtoolbar_enabled')) {
L.DomUtil.removeClass(link, 'subtoolbar_enabled');
setTimeout(() => {
this.options.subToolbar._hide();
}, 100);
} else {
L.DomUtil.addClass(link, 'subtoolbar_enabled');
};

L.IconUtil.toggleXlink(link, 'opacities', 'cancel');
L.IconUtil.toggleTitle(link, 'Make Image Transparent', 'Cancel');
},
});
1 change: 1 addition & 0 deletions src/edit/toolbars/DistortableImage.PopupBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ L.DistortableImageOverlay.addInitHook(function() {
L.FreeRotateAction,
L.LockAction,
L.OpacityAction,
L.OpacitiesAction,
L.BorderAction,
L.ExportAction,
L.DeleteAction,
Expand Down
1 change: 1 addition & 0 deletions src/iconsets/ToolbarIconSet.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9472b3f

Please sign in to comment.