Skip to content

Commit

Permalink
Merge layer_view extension (#995)
Browse files Browse the repository at this point in the history
* Add tested version of layer_view extension

* Fix linting errors

* Modify button style to SVG-Edit colour palette
  • Loading branch information
VenissaCarolQuadros authored Sep 27, 2024
1 parent 59690cd commit 49dd041
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/editor/ConfigObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default class ConfigObj {
* @property {boolean} [showGrid=false] Set by `ext-grid.js`; determines whether or not to show the grid by default
* @property {boolean} [show_outside_canvas=true] Defines whether or not elements outside the canvas should be visible. Set and used in `svgcanvas.js`.
* @property {boolean} [selectNew=true] If true, will replace the selection with the current element and automatically select element objects (when not in "path" mode) after they are created, showing their grips (v2.6).
* @property {boolean} [layerView=false] Set for 'ext-layer_view.js'; determines whether or not only current layer is shown by default
* Set and used in `svgcanvas.js` (`mouseUp`).
*/
this.defaultConfig = {
Expand Down Expand Up @@ -160,7 +161,8 @@ export default class ConfigObj {
// EXTENSION (CLIENT VS. SERVER SAVING/OPENING)
avoidClientSide: false, // Deprecated in favor of `avoidClientSideDownload`
avoidClientSideDownload: false,
avoidClientSideOpen: false
avoidClientSideOpen: false,
layerView: false
}

this.curPrefs = {}
Expand All @@ -184,7 +186,8 @@ export default class ConfigObj {
'ext-shapes',
'ext-polystar',
'ext-storage',
'ext-opensave'
'ext-opensave',
'ext-layer_view'
]
this.curConfig = {
// We do not put on defaultConfig to simplify object copying
Expand Down
85 changes: 85 additions & 0 deletions src/editor/extensions/ext-layer_view/ext-layer_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @file ext-layer_view.js
*
* @license MIT
*
*
*/

const name = 'layer_view'

const loadExtensionTranslation = async function (svgEditor) {
let translationModule
const lang = svgEditor.configObj.pref('lang')
try {
translationModule = await import(`./locale/${lang}.js`)
} catch (_error) {
console.warn(`Missing translation (${lang}) for ${name} - using 'en'`)
translationModule = await import('./locale/en.js')
}
svgEditor.i18next.addResourceBundle(lang, name, translationModule.default)
}

export default {
name,
async init (_S) {
const svgEditor = this
const { svgCanvas } = svgEditor
const { $id, $click } = svgCanvas
await loadExtensionTranslation(svgEditor)

const clickLayerView = (e) => {
$id('tool_layerView').pressed = !$id('tool_layerView').pressed
updateLayerView(e)
}

const updateLayerView = (e) => {
const drawing = svgCanvas.getCurrentDrawing()
const curLayer = drawing.getCurrentLayerName()
let layer = drawing.getNumLayers()
while (layer--) {
const name = drawing.getLayerName(layer)
if (name !== curLayer && $id('tool_layerView').pressed) {
drawing.setLayerVisibility(name, false)
} else {
drawing.setLayerVisibility(name, true)
}
}
$id('layerlist').querySelectorAll('tr.layer').forEach(
function (el) {
const layervis = el.querySelector('td.layervis')
const vis = el.classList.contains('layersel') || !$id('tool_layerView').pressed ? 'layervis' : 'layerinvis layervis'
layervis.setAttribute('class', vis)
}
)
}

return {
name: svgEditor.i18next.t(`${name}:name`),
// The callback should be used to load the DOM with the appropriate UI items
layersChanged () {
if ($id('tool_layerView').pressed) {
updateLayerView()
} if (svgEditor.configObj.curConfig.layerView) {
svgEditor.configObj.curConfig.layerView = false
$id('tool_layerView').pressed = true
updateLayerView()
}
},
layerVisChanged () {
if ($id('tool_layerView').pressed) {
$id('tool_layerView').pressed = !$id('tool_layerView').pressed
}
},
callback () {
const buttonTemplate = document.createElement('template')
const title = `${name}:buttons.0.title`
const key = `${name}:buttons.0.key`
buttonTemplate.innerHTML = `
<se-button id="tool_layerView" title="${title}" shortcut="${key}" src="layer_view.svg"></se-button>`
$id('editor_panel').append(buttonTemplate.content.cloneNode(true))
$click($id('tool_layerView'), clickLayerView.bind(this))
}
}
}
}
9 changes: 9 additions & 0 deletions src/editor/extensions/ext-layer_view/locale/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
name: 'layerview',
buttons: [
{
title: 'Enable/Disable Layer View',
key: 'Ctrl+Shift+L'
}
]
}
6 changes: 6 additions & 0 deletions src/editor/images/layer_view.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/editor/panels/LayersPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ class LayersPanel {
})
evt.currentTarget.parentNode.classList.add('layersel')
self.editor.svgCanvas.setCurrentLayer(evt.currentTarget.textContent)
// run extension when different layer is selected from listener
self.editor.svgCanvas.runExtensions(
'layersChanged'
)
evt.preventDefault()
})
element.addEventListener('mouseup', (evt) => {
Expand All @@ -302,6 +306,10 @@ class LayersPanel {
const vis = evt.currentTarget.classList.contains('layerinvis')
self.editor.svgCanvas.setLayerVisibility(name, vis)
evt.currentTarget.classList.toggle('layerinvis')
// run extension if layer visibility is changed from listener
self.editor.svgCanvas.runExtensions(
'layerVisChanged'
)
})
})

Expand All @@ -313,6 +321,10 @@ class LayersPanel {
tlayer.innerHTML = '<td style="color:white">_</td><td/>'
layerlist.append(tlayer)
}
// run extension when layer panel is populated
self.editor.svgCanvas.runExtensions(
'layersChanged'
)
}
}

Expand Down

0 comments on commit 49dd041

Please sign in to comment.