Skip to content

Commit

Permalink
feat(text-comparator): add text comparator plugin and integrate with …
Browse files Browse the repository at this point in the history
…editor
  • Loading branch information
purocean committed Jan 7, 2025
1 parent 03573d8 commit 5b77ba4
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/renderer/components/FileTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default defineComponent({
const fileTabs = computed(() => (tabs.value as Components.FileTabs.Item[]).map(tab => {
if (currentFile.value && tab.key === toUri(currentFile.value)) {
const { status, writeable } = currentFile.value
const { type, status, writeable } = currentFile.value
let mark = ''
if (!isSaved.value) {
Expand All @@ -351,7 +351,7 @@ export default defineComponent({
mark = '!'
} else if (status === 'loaded') {
mark = ''
} else {
} else if (type === 'file') {
mark = ''
}
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import recordRecentDocument from '@fe/plugins/record-recent-document'
import aiCopilot from '@fe/plugins/ai-copilot'
import viewLinks from '@fe/plugins/view-links'
import insertTable from '@fe/plugins/insert-table'
import textComparator from '@fe/plugins/text-comparator'

export default [
buildInRenderers,
Expand Down Expand Up @@ -156,4 +157,5 @@ export default [
aiCopilot,
viewLinks,
insertTable,
textComparator,
]
69 changes: 69 additions & 0 deletions src/renderer/plugins/text-comparator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Plugin } from '@fe/context'
import type { BuildInActions } from '@fe/types'

export default {
name: 'text-comparator',
register: ctx => {
const extensionId = '@yank-note/extension-text-comparator'
const editorDocType = '__comparator' as const
const compareTextActionId: keyof BuildInActions = 'plugin.text-comparator.open-text-comparator'

const TextComparator = ctx.lib.vue.defineComponent({
setup () {
const { h } = ctx.lib.vue
return () => h(
'div',
{ style: 'width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;' },
[
h('a', {
href: 'javascript:void(0)',
onClick: () => {
ctx.showExtensionManager(extensionId)
},
}, ctx.i18n.t('install-extension-tips', extensionId))
]
)
}
})

ctx.editor.registerCustomEditor({
name: 'text-comparator',
displayName: 'Text Comparator',
supportNonNormalFile: true,
component: TextComparator,
hiddenPreview: true,
when: ({ doc }) => {
return doc?.type === editorDocType
}
})

ctx.action.registerAction({
name: compareTextActionId,
description: ctx.i18n.t('status-bar.tool.open-text-comparator'),
forUser: true,
handler: () => {
ctx.doc.switchDoc({
type: editorDocType,
name: 'Text Comparator',
path: '',
repo: ctx.store.state.currentRepo?.name || ''
})
},
})

ctx.statusBar.tapMenus(menus => {
menus['status-bar-tool']?.list?.push(
{
id: compareTextActionId,
type: 'normal',
title: ctx.i18n.t('status-bar.tool.open-text-comparator'),
subTitle: ctx.keybinding.getKeysLabel(compareTextActionId),
onClick: () => {
ctx.action.getActionHandler(compareTextActionId)()
},
order: 100,
},
)
})
}
} as Plugin
8 changes: 4 additions & 4 deletions src/renderer/services/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ async function _switchDoc (doc: Doc | null, opts?: SwitchDocOpts): Promise<void>

logger.debug('switchDoc', doc)

if (doc && doc.type !== 'file') {
if (doc && doc.type === 'dir') {
throw new Error('Invalid document type')
}

Expand All @@ -739,15 +739,15 @@ async function _switchDoc (doc: Doc | null, opts?: SwitchDocOpts): Promise<void>
})

if (doc) {
doc.plain = !!(extensions.supported(doc.name) || resolveDocType(doc.name)?.type?.plain)
doc.plain = doc.type === 'file' && (!!(extensions.supported(doc.name) || resolveDocType(doc.name)?.type?.plain))
doc.absolutePath = getAbsolutePath(doc)
}

await triggerHook('DOC_BEFORE_SWITCH', { doc, opts }, { breakable: true, ignoreError: true })

try {
if (!doc) {
store.state.currentFile = null
if (!doc || doc.type !== 'file') {
store.state.currentFile = doc
store.state.currentContent = ''
triggerHook('DOC_SWITCHED', { doc: null, opts })
return
Expand Down
9 changes: 7 additions & 2 deletions src/renderer/services/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,20 @@ export function switchEditor (name: string) {
/**
* Register a custom editor.
* @param editor Editor
* @param override Override the existing editor
*/
export function registerCustomEditor (editor: CustomEditor) {
export function registerCustomEditor (editor: CustomEditor, override = false) {
if (!editor.component && editor.name !== DEFAULT_MARKDOWN_EDITOR_NAME) {
throw new Error('Editor component is required')
}

// check if the editor is already registered
if (ioc.get('EDITOR_CUSTOM_EDITOR').some(item => item.name === editor.name)) {
throw new Error(`Editor ${editor.name} is already registered`)
if (override) {
removeCustomEditor(editor.name)
} else {
throw new Error(`Editor ${editor.name} is already registered`)
}
}

ioc.register('EDITOR_CUSTOM_EDITOR', editor)
Expand Down
1 change: 1 addition & 0 deletions src/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ export type BuildInActions = {
'plugin.electron-zoom.zoom-out': () => void,
'plugin.electron-zoom.zoom-reset': () => void,
'plugin.view-links.view-document-links': () => void,
'plugin.text-comparator.open-text-comparator': () => void,
'premium.show': (tab?: PremiumTab) => void,
'base.find-in-repository': (query?: FindInRepositoryQuery) => void,
'base.switch-repository-1': () => void,
Expand Down
2 changes: 2 additions & 0 deletions src/share/i18n/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const data = {
'read-only-mode-desc': 'The application is currently in read-only mode and cannot be edited.',
'trigger-suggestions': 'Trigger Suggestions',
'table-of-contents': 'Table of Contents',
'text-comparator': 'Text Comparator',
'premium': {
'confetti': 'Confetti',
'need-purchase': '[%s] Premium is required',
Expand Down Expand Up @@ -265,6 +266,7 @@ const data = {
'share-preview': 'Share Preview',
'print': 'Print Document',
'export': 'Export Document',
'open-text-comparator': 'Open Text Comparator',
},
'document-info': {
'selected': 'Selected',
Expand Down
2 changes: 2 additions & 0 deletions src/share/i18n/languages/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const data: BaseLanguage = {
'read-only-mode-desc': 'В настоящее время приложение находится в режиме "только для чтения".',
'trigger-suggestions': 'Подсказки триггера',
'table-of-contents': 'Содержание',
'text-comparator': 'Сравнение текста',
'premium': {
'confetti': 'Конфетти',
'need-purchase': 'Для [%s] требуется премиум',
Expand Down Expand Up @@ -266,6 +267,7 @@ const data: BaseLanguage = {
'share-preview': 'Поделиться',
'print': 'Печать документа',
'export': 'Экспорт документа',
'open-text-comparator': 'Открыть сравнение текста',
},
'document-info': {
'selected': 'Выделено',
Expand Down
2 changes: 2 additions & 0 deletions src/share/i18n/languages/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const data: BaseLanguage = {
'read-only-mode-desc': '当前是只读模式,不可编辑',
'trigger-suggestions': '触发提示',
'table-of-contents': '目录',
'text-comparator': '文本比较器',
'premium': {
'confetti': '彩色纸屑',
'need-purchase': '[%s] 需要高级版',
Expand Down Expand Up @@ -266,6 +267,7 @@ const data: BaseLanguage = {
'share-preview': '分享预览',
'print': '打印文档',
'export': '导出文档',
'open-text-comparator': '打开文本比较器',
},
'document-info': {
'selected': '已选择',
Expand Down
2 changes: 2 additions & 0 deletions src/share/i18n/languages/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const data: BaseLanguage = {
'read-only-mode-desc': '當前應用處於唯讀模式,不可編輯',
'trigger-suggestions': '觸發提示',
'table-of-contents': '目錄',
'text-comparator': '文本比較器',
'premium': {
'confetti': '彩色紙屑',
'need-purchase': '[%s] 需高級版',
Expand Down Expand Up @@ -266,6 +267,7 @@ const data: BaseLanguage = {
'share-preview': '分享預覽',
'print': '打印文檔',
'export': '導出文檔',
'open-text-comparator': '打開文本比較器',
},
'document-info': {
'selected': '已選擇',
Expand Down

0 comments on commit 5b77ba4

Please sign in to comment.