Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
韦博韬 committed Aug 17, 2021
1 parent 254d9f9 commit 4adeb0b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 87 deletions.
23 changes: 17 additions & 6 deletions src/changeTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
import {
getSelectedLayers,
getArtboards,
getSketchInfo
getSketchInfo,
isSymbolMaster
} from './util/base';
import {
PLUGIN_HS_SIDEBAR,
Expand Down Expand Up @@ -449,10 +450,15 @@ function switchToolBar(context, loginInfo) {
});

// Put buttons in a stack view
const stack = NSStackView.stackViewWithViews(buttons);
let stack = NSStackView.stackViewWithViews(buttons);

// let stack = UILayoutGuide.alloc().init();
// layoutGuide.addLayoutGuide(buttons)


stack.spacing = 25; // 每个元素的间隔
stack.orientation = NSUserInterfaceLayoutOrientationVertical; // NS用户界面布局方向垂直
stack.edgeInsets = NSEdgeInsetsMake(10, 0, 0, 0); // 与顶部的距离是10
stack.edgeInsets = NSEdgeInsetsMake(80, 0, 0, 0); // 与顶部的距离是10

// Add stack view to main container view
container.addSubview(stack);
Expand All @@ -472,9 +478,13 @@ function switchToolBar(context, loginInfo) {
);

container.addConstraints(horizontalConstraints);
// console.log("123");
// console.log(container);


stack
.topAnchor()
.constraintEqualToAnchor(container.safeAreaLayoutGuide().topAnchor())
.constraintEqualToAnchor(container.topAnchor())
.setActive(true);
// ----------

Expand Down Expand Up @@ -567,7 +577,7 @@ function changeUpload() {
// console.log("监听了画板改变");
// console.log(allArtBoards);
webContents
.executeJavaScript(`getArtboardList(${JSON.stringify(info,artboardList)})`)
.executeJavaScript(`getArtboardList(${JSON.stringify({info,artboardList})})`)
.catch(console.error);
}
}
Expand All @@ -579,8 +589,9 @@ function changeRelevence() {
const webContents = browserWindow.webContents;
let allLayers = getSelectedLayers();
let info = getSketchInfo();
let isSymbolMasterLibrary = isSymbolMaster();
webContents
.executeJavaScript(`getLayersList(${JSON.stringify({allLayers,info})})`)
.executeJavaScript(`getLayersList(${JSON.stringify({allLayers,info,isSymbolMasterLibrary})})`)
.catch(console.error);
}
}
Expand Down
181 changes: 106 additions & 75 deletions src/util/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
COMPONENTSYMBOLS,
DESIGNICONSYMBOLS
} from "./constants";
import {threadDictionary} from "../util/common";
import {
threadDictionary
} from "../util/common";
let sketch = require("sketch");
let dom = require("sketch/dom");
let document = sketch.getSelectedDocument();
Expand Down Expand Up @@ -98,6 +100,7 @@ function getSelectefArtboardIds() {
function getBase64_sektchData(ids) {
let arr = [];
ids = JSON.parse(ids);
console.log(ids);
ids.map(artboardId => {
let artboard = sketch.find('Artboard,[id="' + artboardId + '"]');
let artboardBase64 = miniImage(artboard[0]);
Expand All @@ -106,7 +109,7 @@ function getBase64_sektchData(ids) {
// output: false
// });
// let artboardBase64 = `data:image/png;base64,${buffer.toString("base64")}`;

// console.log(artboardData);
console.log("上面是数据");

Expand Down Expand Up @@ -222,36 +225,36 @@ export async function getSymbols_noImage(libraryName, callback) {

// 根据ids获取symbol对应的图片列表
async function getImagesByIds(symbolIds, libraryName) {

let symbolReferences = await getLibraryIcons(libraryName);
// console.log(symbolReferences);

let symbolReferenceList = symbolReferences.filter(item => symbolIds.indexOf(item.id) > -1);
let promiseList = [];
symbolReferenceList.forEach(symbolReference => {
if (symbolReference) {
promiseList.push(new Promise((resolve, reject) => {
let symbol = symbolReference.import();
//console.log(symbol);
let newPage = new Page();
newPage.parent = document;
symbol.parent = newPage;
let options = {
formats: "png",
output: false
};
let buffer = sketch.export(symbol, options);
let symboolBase64 = buffer.toString("base64");
// images.push({id:symbol.id,symboolBase64})
newPage.parent = nil; // 删除掉当前页面
newPage.remove();
resolve({
id: symbolReference.id,
symboolBase64
})
}))

promiseList.push(new Promise((resolve, reject) => {

let symbol = symbolReference.import();
//console.log(symbol);
let newPage = new Page();
newPage.parent = document;
symbol.parent = newPage;
let options = {
formats: "png",
output: false
};
let buffer = sketch.export(symbol, options);
let symboolBase64 = buffer.toString("base64");
// images.push({id:symbol.id,symboolBase64})
newPage.parent = nil; // 删除掉当前页面
newPage.remove();
resolve({
id: symbolReference.id,
symboolBase64
})
}))
}
})
let images = await Promise.all(promiseList);
Expand Down Expand Up @@ -280,33 +283,33 @@ async function getLibraryIcons(libraryName) {
// if(threadDictionary[libraryName]){
// return threadDictionary[libraryName] || [];
// }
let sketch = require("sketch/dom");
let Library = sketch.Library;
const getRemoteLibraryWithRSS = promisify(Library.getRemoteLibraryWithRSS);
//添加HuxBC-globalstyle-icon库
let url = '';
if (libraryName === DESIGNICONSYMBOLS) {
url = 'https://sketchrsslibrary.github.io/sketchrsslibrary/globalstyle/globalstyle-icon.xml';
} else {
url = 'https://sketchrsslibrary.github.io/sketchrsslibrary/component/component.xml';
}
let library = await getRemoteLibraryWithRSS(url);
if (library) {
let document = sketch.getSelectedDocument();
let symbolReferences = library.getImportableSymbolReferencesForDocument(
document
);
// threadDictionary[libraryName] = data;
return symbolReferences;
} else {
console.log("图标库不存在");
return [];
}


let sketch = require("sketch/dom");
let Library = sketch.Library;
const getRemoteLibraryWithRSS = promisify(Library.getRemoteLibraryWithRSS);

//添加HuxBC-globalstyle-icon库
let url = '';
if (libraryName === DESIGNICONSYMBOLS) {
url = 'https://sketchrsslibrary.github.io/sketchrsslibrary/globalstyle/globalstyle-icon.xml';
} else {
url = 'https://sketchrsslibrary.github.io/sketchrsslibrary/component/component.xml';
}

let library = await getRemoteLibraryWithRSS(url);
if (library) {
let document = sketch.getSelectedDocument();
let symbolReferences = library.getImportableSymbolReferencesForDocument(
document
);
// threadDictionary[libraryName] = data;
return symbolReferences;
} else {
console.log("图标库不存在");
return [];
}


}

Expand Down Expand Up @@ -356,41 +359,69 @@ function getSketchInfo() {
}


function miniImage(selection){
// let sketch = require('sketch')
// let doc = sketch.getSelectedDocument()
// let selection = doc.selectedLayers.layers[0]
function miniImage(selection) {
// let sketch = require('sketch')
// let doc = sketch.getSelectedDocument()
// let selection = doc.selectedLayers.layers[0]

let Slice = sketch.Slice
let exportSlice = new Slice({
frame: selection.frame,
parent: selection.parent
})

let shrinkedSlice = shrinkSlice_by(exportSlice, 30)

let Slice = sketch.Slice
let exportSlice = new Slice({
frame: selection.frame,
parent: selection.parent
})
function shrinkSlice_by(slice, value) {
let frame = slice.frame
frame.x = frame.x + value
frame.y = frame.y + value
frame.width = frame.width - (value * 2)
frame.height = frame.height - (value * 2)
slice.frame = frame
return slice
}

let shrinkedSlice = shrinkSlice_by(exportSlice,30)
let buffer = sketch.export(shrinkedSlice, {
formats: 'png',
output: false
});
exportSlice.parent = null
return `data:image/png;base64,${buffer.toString("base64")}`;

function shrinkSlice_by(slice,value) {
let frame = slice.frame
frame.x = frame.x + value
frame.y = frame.y + value
frame.width = frame.width - (value*2)
frame.height = frame.height - (value*2)
slice.frame = frame
return slice
}

let buffer = sketch.export(shrinkedSlice, { formats: 'png' ,output:false});
exportSlice.parent = null
return `data:image/png;base64,${buffer.toString("base64")}`;


// 判断是否是控件页
function isSymbolMaster() {
let flag = false;
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let pages = doc.pages

let page = pages.filter(item => item.selected === true)[0];
let list = page.layers;
for (let i in list) {
if (list[i].type === 'SymbolMaster') {
flag = true;
break;
}
}
return flag;
}





export {
getArtboards,
getSelectedLayers,
getSymbols,
getImagesByIds,
getSketchInfo,
getBase64_sektchData,
getSelectefArtboardIds
getSelectefArtboardIds,
isSymbolMaster
};
4 changes: 2 additions & 2 deletions src/util/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2021-04-01 16:33:02
* @LastEditTime: 2021-08-13 11:08:56
* @LastEditTime: 2021-08-17 17:50:16
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /sketch/hs-plugin/src/util/common.js
Expand All @@ -14,7 +14,7 @@ import { PLUGINS_HS_LOGIN,PLUGINS_HS_USERINFO } from './constants';
import UI from 'sketch/ui'

//let baseUrl = 'http://localhost:8008/#/webview';
let baseUrl = 'http://10.20.145.85:8008/#/webview';
let baseUrl = 'http://10.20.145.85:8008/#/webview';
// 线程字典
let threadDictionary = NSThread.mainThread().threadDictionary();

Expand Down
4 changes: 2 additions & 2 deletions src/util/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ export const createBrowerWindow = (params) => {
// threadDictionary[options.identifier] = browserWindow;
let window = NSApp.mainWindow();
browserWindow.setAlwaysOnTop(true);
browserWindow.isMovable(true);
browserWindow.isMovable(false);
let sidebar = threadDictionary[PLUGIN_HS_SIDEBAR];
//侧边栏相对app的x坐标
let relativeToApp = sidebar.superview().frame();
let pos = window.convertPointToScreen(relativeToApp.origin)
browserWindow.setSize(params.width, params.height);
browserWindow.setPosition(pos.x - params.width, pos.y,true);
browserWindow.setPosition(pos.x - params.width, pos.y);
browserWindow.setMovable(true);
// browserWindow.once("ready-to-show", () => {
// browserWindow.show();
Expand Down
7 changes: 5 additions & 2 deletions src/util/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
getSymbols_noImage,
getImagesByIds,
getSketchInfo,
getBase64_sektchData
getBase64_sektchData,
isSymbolMaster
} from "./base";
import {
DESIGNICONSYMBOLS,
Expand Down Expand Up @@ -233,6 +234,7 @@ function implementFunc(browserWindow, title) {
getSymbols_noImage(COMPONENTSYMBOLS, (symbolReferences) => {
// let list = symbolReferences.filter(item => item.symbolReference.name.indexOf("common") < 0);
componentList = symbolReferences.filter(item => item.name.indexOf("common") < 0);

webContents
.executeJavaScript(`getSymbols(${JSON.stringify(componentList)})`)
.catch(console.error);
Expand Down Expand Up @@ -470,9 +472,10 @@ function implementFunc(browserWindow, title) {
if (title === "relevence") {
let allLayers = getSelectedLayers();
let info = getSketchInfo();
let isSymbolMasterLibrary = isSymbolMaster();

webContents
.executeJavaScript(`getLayersList(${JSON.stringify({allLayers,info})})`)
.executeJavaScript(`getLayersList(${JSON.stringify({allLayers,info,isSymbolMasterLibrary})})`)
.catch(console.error);


Expand Down

0 comments on commit 4adeb0b

Please sign in to comment.