From c01c65cbbab42c1e9f65ed305e151322ff21c7fe Mon Sep 17 00:00:00 2001 From: enyaxu <40362568+enyaxu@users.noreply.github.com> Date: Sun, 12 Aug 2018 20:55:48 +0800 Subject: [PATCH] #421 Add experiment function RTL support (#439) * feature: Add experiment RTL support * fix: binding to currentfile textdirection * feature: add sourcecode RTL support * feature: add text direction menu upgrade * fix sourceCode does't change from menu switch text direction --- src/main/actions/edit.js | 8 +++++ src/main/menu.js | 11 +++++++ src/main/menus/edit.js | 19 +++++++++++ src/main/utils/filesystem.js | 10 +++++- src/main/window.js | 9 +++-- src/muya/lib/codeMirror/index.js | 4 +++ src/renderer/app.vue | 6 +++- .../components/editorWithTabs/editor.vue | 8 ++++- .../components/editorWithTabs/index.vue | 6 ++++ .../components/editorWithTabs/sourceCode.vue | 17 ++++++++-- src/renderer/store/editor.js | 33 +++++++++++++++++-- src/renderer/store/help.js | 7 ++-- static/preference.md | 5 ++- 13 files changed, 130 insertions(+), 13 deletions(-) diff --git a/src/main/actions/edit.js b/src/main/actions/edit.js index 01de5204..c76c7c8e 100644 --- a/src/main/actions/edit.js +++ b/src/main/actions/edit.js @@ -42,6 +42,10 @@ ipcMain.on('AGANI::update-line-ending-menu', (e, lineEnding) => { appMenu.updateLineEndingnMenu(lineEnding) }) +ipcMain.on('AGANI::update-text-direction-menu', (e, textDirection) => { + appMenu.updateTextDirectionMenu(textDirection) +}) + export const edit = (win, type) => { win.webContents.send('AGANI::edit', { type }) } @@ -57,3 +61,7 @@ export const insertImage = (win, type) => { win.webContents.send('AGANI::INSERT_IMAGE', { type }) } } + +export const textDirection = (win, textDirection) => { + win.webContents.send('AGANI::set-text-direction', { textDirection }) +} diff --git a/src/main/menu.js b/src/main/menu.js index 4f4847f2..6b9fe8c2 100644 --- a/src/main/menu.js +++ b/src/main/menu.js @@ -107,6 +107,17 @@ class AppMenu { lfMenu.checked = true } } + + updateTextDirectionMenu (textDirection) { + const menus = Menu.getApplicationMenu() + const ltrMenu = menus.getMenuItemById('textDirectionLTRMenuEntry') + const rtlMenu = menus.getMenuItemById('textDirectionRTLMenuEntry') + if (textDirection === 'ltr') { + ltrMenu.checked = true + } else { + rtlMenu.checked = true + } + } } export default new AppMenu() diff --git a/src/main/menus/edit.js b/src/main/menus/edit.js index cdc9e23b..07e9b70a 100755 --- a/src/main/menus/edit.js +++ b/src/main/menus/edit.js @@ -128,5 +128,24 @@ export default { actions.lineEnding(browserWindow, 'lf') } }] + }, { + type: 'separator' + }, { + label: 'Text Direction', + submenu: [{ + id: 'textDirectionLTRMenuEntry', + label: 'Left-To-Right', + type: 'radio', + click (menuItem, browserWindow) { + actions.textDirection(browserWindow, 'ltr') + } + }, { + id: 'textDirectionRTLMenuEntry', + label: 'Right-To-Left', + type: 'radio', + click (menuItem, browserWindow) { + actions.textDirection(browserWindow, 'rtl') + } + }] }] } diff --git a/src/main/utils/filesystem.js b/src/main/utils/filesystem.js index 553d38ed..7662aa34 100644 --- a/src/main/utils/filesystem.js +++ b/src/main/utils/filesystem.js @@ -11,6 +11,11 @@ export const getOsLineEndingName = () => { return endOfLine === 'crlf' || isWindows ? 'crlf' : 'lf' } +export const getDefaultTextDirection = () => { + const { textDirection } = userPreference.getAll() + return textDirection +} + const getLineEnding = lineEnding => { if (lineEnding === 'lf') { return '\n' @@ -78,6 +83,8 @@ export const loadMarkdownFile = async pathname => { const filename = path.basename(pathname) + const textDirection = getDefaultTextDirection() + return { markdown, filename, @@ -85,6 +92,7 @@ export const loadMarkdownFile = async pathname => { isUtf8BomEncoded, lineEnding, adjustLineEndingOnSave, - isMixed + isMixed, + textDirection } } diff --git a/src/main/window.js b/src/main/window.js index 90169fbd..7d7d04ad 100644 --- a/src/main/window.js +++ b/src/main/window.js @@ -1,7 +1,7 @@ import path from 'path' import { app, BrowserWindow, screen } from 'electron' import windowStateKeeper from 'electron-window-state' -import { getOsLineEndingName, loadMarkdownFile } from './utils/filesystem' +import { getOsLineEndingName, loadMarkdownFile, getDefaultTextDirection } from './utils/filesystem' import appMenu from './menu' import Watcher from './watcher' import { isMarkdownFile, isDirectory, log } from './utils' @@ -78,10 +78,12 @@ class AppWindow { isUtf8BomEncoded, lineEnding, adjustLineEndingOnSave, - isMixed + isMixed, + textDirection } = data appMenu.updateLineEndingnMenu(lineEnding) + appMenu.updateTextDirectionMenu(textDirection) win.webContents.send('AGANI::open-single-file', { markdown, filename, @@ -108,11 +110,13 @@ class AppWindow { // open a window but do not open a file or directory } else { const lineEnding = getOsLineEndingName() + const textDirection = getDefaultTextDirection() win.webContents.send('AGANI::open-blank-window', { lineEnding, ignoreSaveStatus: true }) appMenu.updateLineEndingnMenu(lineEnding) + appMenu.updateTextDirectionMenu(textDirection) } }) @@ -123,6 +127,7 @@ class AppWindow { this.focusedWindowId = win.id win.webContents.send('AGANI::req-update-line-ending-menu') win.webContents.send('AGANI::request-for-view-layout') + win.webContents.send('AGANI::req-update-text-direction-menu') } }) diff --git a/src/muya/lib/codeMirror/index.js b/src/muya/lib/codeMirror/index.js index dd49612f..e88bb0bf 100644 --- a/src/muya/lib/codeMirror/index.js +++ b/src/muya/lib/codeMirror/index.js @@ -143,4 +143,8 @@ export const setMode = (doc, text) => { }) } +export const setTextDirection = (cm, textDirection) => { + cm.setOption('direction', textDirection) +} + export default codeMirror diff --git a/src/renderer/app.vue b/src/renderer/app.vue index 2d7b8964..f9a72712 100644 --- a/src/renderer/app.vue +++ b/src/renderer/app.vue @@ -23,6 +23,7 @@ :theme="theme" :source-code="sourceCode" :show-tab-bar="showTabBar" + :text-direction="textDirection" > state.editor.currentFile.isSaved, 'markdown': state => state.editor.currentFile.markdown, 'cursor': state => state.editor.currentFile.cursor, - 'wordCount': state => state.editor.currentFile.wordCount + 'wordCount': state => state.editor.currentFile.wordCount, + 'textDirection': state => state.editor.currentFile.textDirection }), ...mapState([ 'windowActive', 'platform', 'init' @@ -130,6 +132,8 @@ dispatch('LISTEN_FOR_NEW_TAB') dispatch('LISTEN_FOR_CLOSE_TAB') dispatch('LINTEN_FOR_EXPORT_SUCCESS') + dispatch('LISTEN_FOR_SET_TEXT_DIRECTION') + dispatch('LISTEN_FOR_TEXT_DIRECTION_MENU') // module: notification dispatch('LISTEN_FOR_NOTIFICATION') } diff --git a/src/renderer/components/editorWithTabs/editor.vue b/src/renderer/components/editorWithTabs/editor.vue index 4c8f56f2..9b0dbd13 100644 --- a/src/renderer/components/editorWithTabs/editor.vue +++ b/src/renderer/components/editorWithTabs/editor.vue @@ -4,6 +4,7 @@ :class="[{ 'typewriter': typewriter, 'focus': focus, 'source': sourceCode }, theme]" :style="{ 'color': theme === 'dark' ? darkColor : lightColor, 'lineHeight': lineHeight, 'fontSize': fontSize, 'font-family': editorFontFamily ? `${editorFontFamily}, ${defaultFontFamily}` : `${defaultFontFamily}` }" + :dir="textDirection" >
@@ -47,6 +49,10 @@ showTabBar: { type: Boolean, required: true + }, + textDirection: { + type: String, + required: true } }, components: { diff --git a/src/renderer/components/editorWithTabs/sourceCode.vue b/src/renderer/components/editorWithTabs/sourceCode.vue index 38e6cd8e..ece77978 100644 --- a/src/renderer/components/editorWithTabs/sourceCode.vue +++ b/src/renderer/components/editorWithTabs/sourceCode.vue @@ -8,7 +8,7 @@