Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Editing #12

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
package-lock.json
4 changes: 2 additions & 2 deletions src/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import {tools} from './tool.js';

/*
/**
* Creates a new canvas with context
*/
export const createCanvas = (width, height, className) => {
Expand All @@ -45,7 +45,7 @@ export const createCanvas = (width, height, className) => {
];
};

/*
/**
* Binds and resolves events to canvas actions
*/
export const createCanvasActions = ({
Expand Down
32 changes: 28 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {createCanvas, createCanvasActions} from './canvas.js';
import {popupFactory} from './popup.js';
import {tools} from './tool.js';

/*
/**
* Creates the toolbar buttons
*/
const createToolButtons = (current, actions, proc, __) => Object.keys(tools)
Expand All @@ -62,7 +62,7 @@ const createToolButtons = (current, actions, proc, __) => Object.keys(tools)
onclick: () => actions.buttonTool(name)
}));

/*
/**
* Creates file menu items
*/
const createFileMenu = (current, actions, _) => ([
Expand All @@ -73,15 +73,15 @@ const createFileMenu = (current, actions, _) => ([
{label: _('LBL_QUIT'), onclick: () => actions.menuQuit()}
]);

/*
/**
* Creates the image menu items
*/
const createImageMenu = (actions, __) => ([
{label: __('LBL_RESIZE_SCALED'), onclick: () => actions.menuResizeImage()},
{label: __('LBL_RESIZE_CANVAS'), onclick: () => actions.menuResizeCanvas()}
]);

/*
/**
* Creates the application
*/
const createApplication = (core, proc, win, $content) => {
Expand All @@ -97,6 +97,16 @@ const createApplication = (core, proc, win, $content) => {
}
});

const fontDialog = (font, cb) => core.make('osjs/dialog', 'font', {
name: font.name,
size: font.size,
style: font.style
}, (btn, value) => {
if (btn === "ok") {
cb(value);
}
});

const basic = core.make('osjs/basic-application', proc, win, {
defaultFilename: 'New Image.png'
});
Expand All @@ -121,6 +131,10 @@ const createApplication = (core, proc, win, $content) => {
orientation: 'horizontal'
}, [
...createToolButtons(state.tool, actions, proc, __),
h(Button, {
label: __('LBL_SET_FONT'),
onclick: () => actions.buttonFont()
}),
h(Button, {}, [
h('div', {
style: {height: '1em', backgroundColor: state.tool.foreground},
Expand Down Expand Up @@ -200,6 +214,11 @@ const createApplication = (core, proc, win, $content) => {
},
tool: {
name: 'pointer',
font: {
name: 'arial',
size: 10,
style: 'regular',
},
foreground: '#000000',
background: '#ffffff',
stroke: false,
Expand All @@ -224,6 +243,10 @@ const createApplication = (core, proc, win, $content) => {
actions.setSize({width, height});
}),

buttonFont: () => (state, actions) => fontDialog(state.tool.font, font => {
actions.setFont(font);
}),

buttonForeground: () => (state, actions) => colorDialog(state.tool.foreground, color => {
actions.setForeground(color);
}),
Expand Down Expand Up @@ -263,6 +286,7 @@ const createApplication = (core, proc, win, $content) => {

setSize: size => state => ({image: size}),
setTool: name => state => ({tool: Object.assign({}, state.tool, {name})}),
setFont: font => state => ({tool: Object.assign({}, state.tool, {font})}),
setForeground: foreground => state => ({tool: Object.assign({}, state.tool, {foreground})}),
setBackground: background => state => ({tool: Object.assign({}, state.tool, {background})}),
setStroke: stroke => state => ({tool: Object.assign({}, state.tool, {stroke})}),
Expand Down
14 changes: 14 additions & 0 deletions src/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export const en_EN = {
LBL_PICKER: 'Picker',
LBL_FILL: 'Fill',
LBL_PENCIL: 'Pencil',
LBL_TEXT: 'Text',
LBL_RECTANGLE: 'Rectangle',
LBL_SQUARE: 'Square',
LBL_OVAL: 'Oval',
LBL_CIRCLE: 'Circle',
LBL_SET_FONT: 'Set Font',
LBL_STROKE: 'Stroke',
LBL_PATH: 'Path'
};
Expand All @@ -20,10 +22,12 @@ export const sv_SE = {
LBL_PICKER: 'Plockare',
LBL_FILL: 'Fylla',
LBL_PENCIL: 'Penna',
LBL_TEXT: 'Text',
LBL_RECTANGLE: 'Rektangel',
LBL_SQUARE: 'Fyrkant',
LBL_OVAL: 'Oval',
LBL_CIRCLE: 'Cirkel',
LBL_SET_FONT: 'Ställ in teckensnitt',
LBL_STROKE: 'Stroke',
LBL_PATH: 'Väg'
};
Expand All @@ -35,10 +39,12 @@ export const nb_NO = {
LBL_PICKER: 'Fargevalg',
LBL_FILL: 'Fyll',
LBL_PENCIL: 'Blyant',
LBL_TEXT: 'Tekst',
LBL_RECTANGLE: 'Rektangel',
LBL_SQUARE: 'Firkant',
LBL_OVAL: 'Oval',
LBL_CIRCLE: 'Sirkel',
LBL_SET_FONT: 'Angi skrifttype',
LBL_STROKE: 'Strøk',
LBL_PATH: 'Sti'
};
Expand All @@ -50,10 +56,12 @@ export const vi_VN = {
LBL_PICKER: 'Chọn màu',
LBL_FILL: 'Tô màu',
LBL_PENCIL: 'Bút chì',
LBL_TEXT: 'Văn bản',
LBL_RECTANGLE: 'Hình chữ nhật',
LBL_SQUARE: 'Hình vuông',
LBL_OVAL: 'Hình trái xoan',
LBL_CIRCLE: 'Hình tròn',
LBL_SET_FONT: 'Thiết lập phông chữ',
LBL_STROKE: 'Độ nét',
LBL_PATH: 'Đường kẻ'
};
Expand All @@ -65,10 +73,12 @@ export const pt_BR = {
LBL_PICKER: 'Selecionador de cores',
LBL_FILL: 'Preencher com cor',
LBL_PENCIL: 'Lápis',
LBL_TEXT: 'Texto',
LBL_RECTANGLE: 'Retângulo',
LBL_SQUARE: 'Quadrado',
LBL_OVAL: 'Oval',
LBL_CIRCLE: 'Circulo',
LBL_SET_FONT: 'Definir fonte',
LBL_STROKE: 'Contorno',
LBL_PATH: 'Linha'
};
Expand All @@ -80,10 +90,12 @@ export const fr_FR = {
LBL_PICKER: 'Sélectionner la couleur',
LBL_FILL: 'Remplir',
LBL_PENCIL: 'Crayon',
LBL_TEXT: 'Texte',
LBL_RECTANGLE: 'Rectangle',
LBL_SQUARE: 'Carré',
LBL_OVAL: 'Ellipse',
LBL_CIRCLE: 'Cercle',
LBL_SET_FONT: 'Définir la police',
LBL_STROKE: 'Contour',
LBL_PATH: 'Ligne'
};
Expand All @@ -95,10 +107,12 @@ export const tr_TR = {
LBL_PICKER: 'Renk Seçim Aracı',
LBL_FILL: 'Doldur',
LBL_PENCIL: 'Kalem',
LBL_TEXT: 'Metin',
LBL_RECTANGLE: 'Dörtgen',
LBL_SQUARE: 'Kare',
LBL_OVAL: 'Oval',
LBL_CIRCLE: 'Daire',
LBL_SET_FONT: 'Yazı Tipini Ayarla',
LBL_STROKE: 'Kalınlık',
LBL_PATH: 'Yol'
};
20 changes: 19 additions & 1 deletion src/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import iconPicker from './icons/stock-color-pick-from-screen-16.png';
import iconFile from './icons/stock-tool-bucket-fill-16.png';
import iconPath from './icons/stock-tool-path-16.png';
import iconPencil from './icons/stock-tool-pencil-16.png';
import iconText from './icons/stock-tool-text-16.png';
import iconRectangle from './icons/stock-shape-rectangle-16.png';
import iconSquare from './icons/stock-shape-square-16.png';
import iconOval from './icons/stock-shape-ellipse-16.png';
Expand All @@ -44,7 +45,7 @@ const rgbToHex = (r, g, b) => '#' + [
parseInt(b, 10).toString(16)
].map(val => String(val).padStart(2, '0')).join('');

/*
/**
* The list of tools
*/
export const tools = {
Expand Down Expand Up @@ -126,6 +127,23 @@ export const tools = {
}
},

text: {
label: 'LBL_TEXT',
icon: iconText,

// TODO: Open text input somehow, either here or from `index.js`
mousedown: ({tool, tempContext}) => {
tempContext.strokeStyle = tool.foreground;
},

mousemove: ({tool, tempContext, current, diff, start, width, height}) => {
tempContext.font = `${tool.font.size}px ${tool.font.name}`;

// TODO: Implement a way to get text from the user
tempContext.fillText(tool.text, start.x, start.y);
}
},

rect: {
label: 'LBL_RECTANGLE',
icon: iconRectangle,
Expand Down