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

Apply "quick import" to "+" tab and allow multiple file selection #157

Merged
merged 2 commits into from
Jan 28, 2025
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
32 changes: 1 addition & 31 deletions reascripts/ReaSpeech/source/ui/ASRActions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,42 +106,12 @@ function ASRActions:import_button()

self._import_button = Widgets.Button.new({
label = "Import Transcript",
on_click = self._import_click
on_click = TranscriptImporter:quick_import()
})

return self._import_button
end

ASRActions._import_click = function()
local filenames = Widgets.FileSelector.simple_open(
'Import Transcript',
{ json = 'JSON Files' }
)

local importer = app.plugins(ASRPlugin:key()):importer()

if #filenames < 1 then
importer:present()
return
end

-- only considering one selection for the moment
local selection = filenames[1]

local transcript, err = importer:import(selection)

if not transcript or err then
importer:present()
else
local plugin = TranscriptUI.new {
app = app,
transcript = transcript,
_transcript_saved = true
}
app.plugins:add_plugin(plugin)
end
end

function ASRActions.pluralizer(count, suffix)
if count == 0 then
return '', suffix
Expand Down
61 changes: 61 additions & 0 deletions reascripts/ReaSpeech/source/ui/TranscriptImporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,64 @@ function TranscriptImporter:import(filepath)

return transcript
end

function TranscriptImporter:quick_import()
return function()
local filenames = Widgets.FileSelector.simple_open(
'Import Transcript',
{ json = 'JSON Files' },
{ allow_multiple = true }
)

if #filenames < 1 then
local importer = app.plugins(ASRPlugin:key()):importer()
importer:present()
return
end

local valid_filenames = {}
for _, filename in ipairs(filenames) do
if filename and filename ~= '' then
table.insert(valid_filenames, filename)
end
end

local load_errors = {}

for _, filename in ipairs(valid_filenames) do
local can_import, msg = self:can_import(filename)

if not can_import then
table.insert(load_errors, {filename, msg})
else
local transcript, err = self:import(filename)

if not transcript or err then
table.insert(load_errors, {filename, err})
else
local plugin = TranscriptUI.new {
app = app,
transcript = transcript,
_transcript_saved = true
}
app.plugins:add_plugin(plugin)
end
end
end

if #load_errors > 0 then
local title = 'Import complete, but...'
local msg = 'Some selected files were not loaded:\n\n%s'

local messages = {}

for _, err in ipairs(load_errors) do
table.insert(messages, PathUtil.get_filename(err[1]) .. ': ' .. err[2])
end

local file_messages = table.concat(messages, '\n')

app.alert_popup:show(title, msg:format(file_messages))
end
end
end
4 changes: 1 addition & 3 deletions reascripts/ReaSpeech/source/ui/TranscriptUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ function TranscriptUI:new_tab_menu()

return {
{ label = "Load Transcript",
on_click = function()
self.app.plugins(ASRPlugin:key()):importer():open()
end
on_click = TranscriptImporter:quick_import()
},
}
end
Expand Down
Loading