#88 Extension modification (#90)

master
Agriya Dev5 2021-04-19 23:33:38 +05:30 committed by GitHub
parent 8daae6dcdd
commit d58ae7881e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 51 deletions

View File

@ -24,21 +24,21 @@ export default {
async init (S) { async init (S) {
const svgEditor = this; const svgEditor = this;
const {svgCanvas} = svgEditor; const {svgCanvas} = svgEditor;
const {getElem} = svgCanvas; const {getElem, $id} = svgCanvas;
const {$, svgroot} = S, const {$, svgroot} = S,
addElem = svgCanvas.addSVGElementFromJson, addElem = svgCanvas.addSVGElementFromJson,
selManager = S.selectorManager; selManager = S.selectorManager;
let startX, let startX;
startY, let startY;
curLine, let curLine;
startElem, let startElem;
endElem, let endElem;
seNs, let seNs;
{svgcontent} = S, let {svgcontent} = S;
started = false, let started = false;
connections = [], let connections = [];
selElems = []; let selElems = [];
/** /**
* *
@ -48,7 +48,7 @@ export default {
* @param {Float} offset * @param {Float} offset
* @returns {module:math.XYObject} * @returns {module:math.XYObject}
*/ */
function getBBintersect (x, y, bb, offset) { const getBBintersect = (x, y, bb, offset) => {
if (offset) { if (offset) {
offset -= 0; offset -= 0;
bb = $.extend({}, bb); bb = $.extend({}, bb);
@ -85,7 +85,7 @@ export default {
* @param {Element} line * @param {Element} line
* @returns {Float} * @returns {Float}
*/ */
function getOffset (side, line) { const getOffset = (side, line) => {
const giveOffset = line.getAttribute('marker-' + side); const giveOffset = line.getAttribute('marker-' + side);
// TODO: Make this number (5) be based on marker width/height // TODO: Make this number (5) be based on marker width/height
@ -97,7 +97,7 @@ export default {
* @param {boolean} on * @param {boolean} on
* @returns {void} * @returns {void}
*/ */
function showPanel (on) { const showPanel = (on) => {
let connRules = $id('connector_rules'); let connRules = $id('connector_rules');
if (!connRules) { if (!connRules) {
connRules = document.createElement('style'); connRules = document.createElement('style');
@ -105,6 +105,7 @@ export default {
document.getElementsByTagName("head")[0].appendChild(connRules); document.getElementsByTagName("head")[0].appendChild(connRules);
} }
connRules.textContent = (!on ? '' : '#tool_clone, #tool_topath, #tool_angle, #xy_panel { display: none !important; }'); connRules.textContent = (!on ? '' : '#tool_clone, #tool_topath, #tool_angle, #xy_panel { display: none !important; }');
if($id('connector_panel'))
$id('connector_panel').style.display = (on) ? 'block' : 'none'; $id('connector_panel').style.display = (on) ? 'block' : 'none';
} }
@ -116,7 +117,7 @@ export default {
* @param {boolean} [setMid] * @param {boolean} [setMid]
* @returns {void} * @returns {void}
*/ */
function setPoint (elem, pos, x, y, setMid) { const setPoint = (elem, pos, x, y, setMid) => {
const pts = elem.points; const pts = elem.points;
const pt = svgroot.createSVGPoint(); const pt = svgroot.createSVGPoint();
pt.x = x; pt.x = x;
@ -149,7 +150,7 @@ export default {
* @param {Float} diffY * @param {Float} diffY
* @returns {void} * @returns {void}
*/ */
function updateLine (diffX, diffY) { const updateLine = (diffX, diffY) => {
// Update line with element // Update line with element
let i = connections.length; let i = connections.length;
while (i--) { while (i--) {
@ -188,7 +189,7 @@ export default {
* @param {Element[]} [elems=selElems] Array of elements * @param {Element[]} [elems=selElems] Array of elements
* @returns {void} * @returns {void}
*/ */
function findConnectors (elems = selElems) { const findConnectors = (elems = selElems) => {
// const connectors = svgcontent.querySelectorAll('.se_connector'); // const connectors = svgcontent.querySelectorAll('.se_connector');
const connectors = svgcontent.querySelectorAll('.se_connector'); const connectors = svgcontent.querySelectorAll('.se_connector');
connections = []; connections = [];
@ -216,7 +217,7 @@ export default {
addThis = false; addThis = false;
// The connected element might be part of a selected group // The connected element might be part of a selected group
parents = svgCanvas.getParents(cElem.parentNode); const parents = svgCanvas.getParents(cElem.parentNode);
Array.prototype.forEach.call(parents, function(el, i){ Array.prototype.forEach.call(parents, function(el, i){
if (elems.includes(el)) { if (elems.includes(el)) {
// Pretend this element is selected // Pretend this element is selected
@ -246,7 +247,7 @@ export default {
* @param {Element[]} [elems=selElems] * @param {Element[]} [elems=selElems]
* @returns {void} * @returns {void}
*/ */
function updateConnectors (elems) { const updateConnectors = (elems) => {
// Updates connector lines based on selected elements // Updates connector lines based on selected elements
// Is not used on mousemove, as it runs getStrokedBBox every time, // Is not used on mousemove, as it runs getStrokedBBox every time,
// which isn't necessary there. // which isn't necessary there.
@ -324,7 +325,7 @@ export default {
* Do on reset. * Do on reset.
* @returns {void} * @returns {void}
*/ */
function init () { const init = () => {
// Make sure all connectors have data set // Make sure all connectors have data set
const elements = svgcontent.querySelectorAll('*'); const elements = svgcontent.querySelectorAll('*');
elements.forEach(function (curthis) { elements.forEach(function (curthis) {
@ -343,23 +344,22 @@ export default {
}); });
} }
const buttons = [{
id: 'mode_connect',
type: 'mode_flyout',
events: {
click () {
svgCanvas.setMode('connector');
}
}
}];
const strings = await loadExtensionTranslation(svgEditor.configObj.pref('lang')); const strings = await loadExtensionTranslation(svgEditor.configObj.pref('lang'));
return { return {
/** @todo JFH special flag */ /** @todo JFH special flag */
newUI: true, newUI: true,
name: strings.name, name: strings.name,
buttons: strings.buttons.map((button, i) => { callback () {
return Object.assign(buttons[i], button); // Add the button and its handler(s)
}), const buttonTemplate = document.createElement("template");
buttonTemplate.innerHTML = `
<se-button id="mode_connect" title="Connect two objects" src="./images/conn.svg"></se-button>
`
$id('tools_left').append(buttonTemplate.content.cloneNode(true));
$id('mode_connect').addEventListener("click", () => {
svgCanvas.setMode('connector');
});
},
/* async */ addLangData ({lang}) { // , importLocale: importLoc /* async */ addLangData ({lang}) { // , importLocale: importLoc
return { return {
data: strings.langList data: strings.langList
@ -384,7 +384,7 @@ export default {
// If child of foreignObject, use parent // If child of foreignObject, use parent
const fo = svgCanvas.getClosest(mouseTarget.parentNode, 'foreignObject'); const fo = svgCanvas.getClosest(mouseTarget.parentNode, 'foreignObject');
startElem = fo.length ? fo[0] : mouseTarget; startElem = fo ? fo : mouseTarget;
// Get center of source element // Get center of source element
const bb = svgCanvas.getStrokedBBox([startElem]); const bb = svgCanvas.getStrokedBBox([startElem]);
@ -444,7 +444,7 @@ export default {
while (slen--) { while (slen--) {
const elem = selElems[slen]; const elem = selElems[slen];
// Look for selected connector elements // Look for selected connector elements
if (elem && dataStorage.get(elem, 'c_start')) { if (elem && dataStorage.has(elem, 'c_start')) {
// Remove the "translate" transform given to move // Remove the "translate" transform given to move
svgCanvas.removeFromSelection([elem]); svgCanvas.removeFromSelection([elem]);
svgCanvas.getTransformList(elem).clear(); svgCanvas.getTransformList(elem).clear();
@ -466,7 +466,7 @@ export default {
return undefined; return undefined;
} }
const fo = svgCanvas.getClosest(mouseTarget.parentNode, 'foreignObject'); const fo = svgCanvas.getClosest(mouseTarget.parentNode, 'foreignObject');
if (fo.length) { mouseTarget = fo[0]; } if (fo) { mouseTarget = fo; }
const parents = svgCanvas.getParents(mouseTarget.parentNode); const parents = svgCanvas.getParents(mouseTarget.parentNode);
@ -481,6 +481,7 @@ export default {
} }
if (parents.indexOf(svgcontent) === -1) { if (parents.indexOf(svgcontent) === -1) {
// Not a valid target element, so remove line // Not a valid target element, so remove line
if(curLine)
curLine.remove(); curLine.remove();
started = false; started = false;
return { return {
@ -492,7 +493,8 @@ export default {
// Valid end element // Valid end element
endElem = mouseTarget; endElem = mouseTarget;
const startId = startElem.id, endId = endElem.id; const startId = (startElem) ? startElem.id : '';
const endId = (endElem) ? endElem.id : '';
const connStr = startId + ' ' + endId; const connStr = startId + ' ' + endId;
const altStr = endId + ' ' + startId; const altStr = endId + ' ' + startId;
// Don't create connector if one already exists // Don't create connector if one already exists
@ -545,7 +547,7 @@ export default {
let i = selElems.length; let i = selElems.length;
while (i--) { while (i--) {
const elem = selElems[i]; const elem = selElems[i];
if (elem && dataStorage.get(elem, 'c_start')) { if (elem && dataStorage.has(elem, 'c_start')) {
selManager.requestSelector(elem).showGrips(false); selManager.requestSelector(elem).showGrips(false);
if (opts.selectedElement && !opts.multiselected) { if (opts.selectedElement && !opts.multiselected) {
// TODO: Set up context tools and hide most regular line tools // TODO: Set up context tools and hide most regular line tools

View File

@ -12,6 +12,7 @@ export default {
init ({$}) { init ({$}) {
const svgEditor = this; const svgEditor = this;
const canv = svgEditor.svgCanvas; const canv = svgEditor.svgCanvas;
const {$id} = canv;
const svgroot = canv.getRootElem(); const svgroot = canv.getRootElem();
let lastBBox = {}; let lastBBox = {};
@ -22,17 +23,19 @@ export default {
let startX; let startX;
let startY; let startY;
const events = {
id: 'tool_shapelib',
click () {
canv.setMode(modeId);
}
};
return { return {
events,
callback () { callback () {
// should later register the event if ($id('tool_shapelib') === null) {
const buttonTemplate = document.createElement("template");
buttonTemplate.innerHTML = `
<se-explorerbutton id="tool_shapelib" title="Shape library" lib="./extensions/ext-shapes/shapelib/"
src="./images/shapelib.svg"></se-explorerbutton>
`
$id('tools_left').append(buttonTemplate.content.cloneNode(true));
$id('tool_shapelib').addEventListener("click", () => {
canv.setMode(modeId);
});
}
}, },
mouseDown (opts) { mouseDown (opts) {
const mode = canv.getMode(); const mode = canv.getMode();

View File

@ -214,12 +214,9 @@ class LeftPanel {
<se-button id="tool_circle" title="Square" src="./images/circle.svg"></se-button> <se-button id="tool_circle" title="Square" src="./images/circle.svg"></se-button>
<se-button id="tool_fhellipse" title="Free-Hand Rectangle" src="./images/fh_ellipse.svg"></se-button> <se-button id="tool_fhellipse" title="Free-Hand Rectangle" src="./images/fh_ellipse.svg"></se-button>
</se-flyingbutton> </se-flyingbutton>
<se-button id="mode_connect" title="Connect two objects" src="./images/conn.svg"></se-button>
<se-button id="tool_text" title="Text Tool" src="./images/text.svg" shortcut="T"></se-button> <se-button id="tool_text" title="Text Tool" src="./images/text.svg" shortcut="T"></se-button>
<se-button id="tool_image" title="Image Tool" src="./images/image.svg"></se-button> <se-button id="tool_image" title="Image Tool" src="./images/image.svg"></se-button>
<se-button id="tool_eyedropper" title="Eye Dropper Tool" src="./images/eye_dropper.svg" shortcut="I"></se-button> <se-button id="tool_eyedropper" title="Eye Dropper Tool" src="./images/eye_dropper.svg" shortcut="I"></se-button>
<se-explorerbutton id="tool_shapelib" title="Shape library" lib="./extensions/ext-shapes/shapelib/"
src="./images/shapelib.svg"></se-explorerbutton>
</div> <!-- tools_left --> </div> <!-- tools_left -->
`; `;
this.editor.$svgEditor.append(template.content.cloneNode(true)); this.editor.$svgEditor.append(template.content.cloneNode(true));