Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ class CreateNoteService {
}

fun createWithContent(
project: Project,
project: Project?,
title: String,
extension: String,
content: String,
): VirtualFile? = createNoteFileAndOpen(project, title, extension, content)
openNote: Boolean = true,
): VirtualFile? = createNoteFileAndOpen(project, title, extension, content, openNote)

private fun createNoteFileAndOpen(
project: Project,
project: Project?,
title: String,
extension: String,
content: String,
openNote: Boolean = true,
): VirtualFile? {
val fileName = createFileName(title, extension)
val file = createNoteFile(fileName, content)
val virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)

if (virtualFile != null) {
addNoteAndOpen(project, title, virtualFile)
addNote(title, virtualFile, project, openNote)
}

return virtualFile
Expand All @@ -61,12 +63,17 @@ class CreateNoteService {
return FileHelper.createFileWithContent(notesDirectory, fileName, content)
}

private fun addNoteAndOpen(
project: Project,
private fun addNote(
title: String,
virtualFile: VirtualFile,
project: Project?,
openNote: Boolean,
) {
NoteStorageRepository.getInstance().addNote(title, virtualFile.path)
FileEditorManager.getInstance(project).openFile(virtualFile, true)
if (openNote) {
project?.let { proj ->
FileEditorManager.getInstance(proj).openFile(virtualFile, true)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.nazarethlabs.codex.service.note

import com.intellij.openapi.vfs.VirtualFile
import com.nazarethlabs.codex.repository.NoteStorageRepository
import com.nazarethlabs.codex.service.settings.NotesSettingsService
import java.io.File

class ImportNotesService {
private val createNoteService = CreateNoteService()

fun importFiles(files: List<File>): List<VirtualFile> {
val importedFiles = mutableListOf<VirtualFile>()
for (file in files) {
if (file.exists() && file.isFile) {
val content = file.readText()
val title = resolveUniqueTitle(file.name)
val extension = getExtension(file)
val virtualFile = createNoteService.createWithContent(null, title, extension, content, false)
if (virtualFile != null) {
importedFiles.add(virtualFile)
}
}
}

return importedFiles
}

private fun resolveUniqueTitle(originalName: String): String {
val notes = NoteStorageRepository.getInstance().getAllNotes()
val title = originalName.substringBeforeLast(".")

if (notes.none { it.title == title }) {
return title
}

var counter = 1
var candidate: String
do {
candidate = "$title ($counter)"
counter++
} while (notes.any { it.title == candidate })

return candidate
}

private fun getExtension(file: File): String {
val extension = file.extension
return if (extension.isNotEmpty()) ".$extension" else NotesSettingsService().getDefaultFileExtension()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.FormBuilder
import com.nazarethlabs.codex.helper.MessageHelper
import com.nazarethlabs.codex.ui.settings.component.NotesConfigExportNotesPanelComponent
import com.nazarethlabs.codex.ui.settings.component.NotesConfigImportNotesPanelComponent
import com.nazarethlabs.codex.ui.settings.component.NotesConfigOpenFolderPanelComponent
import javax.swing.JPanel

class NotesConfigFormComponent {
private var fileExtensionField: JBTextField? = null
private var notesDirectoryField: JBTextField? = null
private val openFolderPanelComponent = NotesConfigOpenFolderPanelComponent()
private val importNotesPanelComponent = NotesConfigImportNotesPanelComponent()
private val exportNotesPanelComponent = NotesConfigExportNotesPanelComponent()

fun build(): JPanel {
Expand Down Expand Up @@ -39,6 +41,8 @@ class NotesConfigFormComponent {
.addComponent(JPanel().apply { preferredSize = java.awt.Dimension(0, 10) })
.addLabeledComponent("", openFolderPanelComponent.createOpenFolderPanel(), 1, false)
.addComponent(JPanel().apply { preferredSize = java.awt.Dimension(0, 10) })
.addLabeledComponent("", importNotesPanelComponent.createImportNotesPanel(), 1, false)
.addComponent(JPanel().apply { preferredSize = java.awt.Dimension(0, 10) })
.addLabeledComponent("", exportNotesPanelComponent.createExportNotesPanel(), 1, false)
.addComponentFillVertically(JPanel(), 0)
.panel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.nazarethlabs.codex.ui.settings.component

import com.intellij.icons.AllIcons
import com.intellij.openapi.ui.Messages
import com.intellij.util.ui.JBUI
import com.nazarethlabs.codex.helper.MessageHelper
import com.nazarethlabs.codex.service.note.ImportNotesService
import javax.swing.BoxLayout
import javax.swing.JButton
import javax.swing.JFileChooser
import javax.swing.JLabel
import javax.swing.JPanel

class NotesConfigImportNotesPanelComponent {
private val importNotesService = ImportNotesService()

fun createImportNotesPanel(): JPanel {
val panel = JPanel()
panel.layout = BoxLayout(panel, BoxLayout.Y_AXIS)
panel.border = JBUI.Borders.empty()

val titleLabel = JLabel(MessageHelper.getMessage("settings.import.notes.title"))
titleLabel.font = titleLabel.font.deriveFont(14.0f).deriveFont(java.awt.Font.BOLD)
panel.add(titleLabel)

val descriptionLabel = JLabel(MessageHelper.getMessage("settings.import.notes.description"))
descriptionLabel.font = descriptionLabel.font.deriveFont(12.0f)
panel.add(descriptionLabel)

panel.add(JPanel().apply { preferredSize = java.awt.Dimension(0, 10) })

panel.add(createImportNotesButton())

return panel
}

private fun createImportNotesButton(): JButton {
val button = JButton(MessageHelper.getMessage("settings.import.notes.button"))
button.icon = AllIcons.ToolbarDecorator.Import
button.toolTipText = MessageHelper.getMessage("settings.import.notes.tooltip")

button.addActionListener {
val fileChooser = JFileChooser()
fileChooser.dialogTitle = MessageHelper.getMessage("settings.import.notes.button")
fileChooser.isMultiSelectionEnabled = true
fileChooser.fileSelectionMode = JFileChooser.FILES_ONLY

val result = fileChooser.showOpenDialog(null)
if (result == JFileChooser.APPROVE_OPTION) {
try {
val selectedFiles = fileChooser.selectedFiles.toList()
val importedFiles = importNotesService.importFiles(selectedFiles)

Messages.showInfoMessage(
MessageHelper.getMessage("settings.import.notes.success", importedFiles.size),
MessageHelper.getMessage("settings.display.name"),
)
} catch (e: Exception) {
Messages.showErrorDialog(
"${MessageHelper.getMessage("settings.import.notes.error")}: ${e.message}",
MessageHelper.getMessage("settings.display.name"),
)
}
}
}

return button
}
}
6 changes: 6 additions & 0 deletions src/main/resources/messages/MyBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ settings.notes.directory.label=Notes directory:
settings.notes.directory.description=Directory used to create new note files
settings.open.notes.folder.button=Open notes folder
settings.open.notes.folder.tooltip=Open the folder where notes are stored
settings.import.notes.title=Import Notes
settings.import.notes.description=Select files to import as notes into the configured notes folder.
settings.import.notes.button=Import notes
settings.import.notes.tooltip=Import files as notes
settings.import.notes.success={0} note(s) imported successfully
settings.import.notes.error=Failed to import notes
settings.export.notes.button=Export all notes
settings.export.notes.tooltip=Export all notes as a ZIP file
settings.export.notes.success=Notes exported successfully to {0}
Expand Down
Loading