| 1 |
|
| 2 |
require_relative 'listview' |
| 3 |
|
| 4 |
Plugin.create(:intent_selector) do |
| 5 |
UserConfig[:intent_selector_rules] ||= [] |
| 6 |
|
| 7 |
on_intent_select do |intents, model| |
| 8 |
case model |
| 9 |
when Diva::Model |
| 10 |
intent_open(intents, model: model) |
| 11 |
else |
| 12 |
intent_open(intents, uri: Diva::URI!(model)) |
| 13 |
end |
| 14 |
end |
| 15 |
|
| 16 |
settings(_('関連付け')) do |
| 17 |
intents = Plugin.filtering(:intent_all, []).first.map{|i|[i.slug.to_s, i.label]}.to_h |
| 18 |
models = Plugin.filtering(:retrievers, []).first.map{|s|[s[:slug].to_s,s[:name]]}.to_h.merge('' => _('(未定義)')) |
| 19 |
listview( |
| 20 |
:intent_selector_rules, |
| 21 |
columns: [ |
| 22 |
[_('開く方法'), ->(record) { intents[record[:intent]] }], |
| 23 |
[_('対象'), ->(record) { models[record[:model]] }], |
| 24 |
[_('条件'), ->(record) { record[:str] }], |
| 25 |
], |
| 26 |
) do |_record| |
| 27 |
select(_('開く方法'), :intent, intents) |
| 28 |
select(_('対象'), :model, models) |
| 29 |
input '条件', :str |
| 30 |
end |
| 31 |
end |
| 32 |
|
| 33 |
中から選び出し、その方法で開く。 |
| 34 |
ルでintentを選出し、一つにintentが定まれば直ちにそれで開く。 |
| 35 |
、intent選択ダイアログを表示して、ユーザに決定を仰ぐ。 |
| 36 |
|
| 37 |
配列 |
| 38 |
らない場合は、省略してもよい |
| 39 |
ている場合は、省略してもよい |
| 40 |
def intent_open(intents, model: nil, uri: model.uri) |
| 41 |
recommended, suggested = divide_intents(intents, uri, specified_model_slug(model)) |
| 42 |
if recommended.size == 1 |
| 43 |
Plugin::Intent::IntentToken.open( |
| 44 |
uri: uri, |
| 45 |
model: model, |
| 46 |
intent: recommended.first, |
| 47 |
parent: nil) |
| 48 |
else |
| 49 |
intent_choose_dialog(recommended + suggested, model: model, uri: uri) |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
def intent_choose_dialog(intents, model: nil, uri: model.uri) |
| 54 |
dialog(_('開く - %{application_name}') % {application_name: Environment::NAME}) do |
| 55 |
set_value( |
| 56 |
intent: intents.first, |
| 57 |
save_uri: uri.to_s, |
| 58 |
) |
| 59 |
label "%{uri}\nを開こうとしています。どの方法で開きますか?" % {uri: uri} |
| 60 |
select nil, :intent, mode: :radio do |
| 61 |
intents.each do |intent| |
| 62 |
option intent, intent.label |
| 63 |
end |
| 64 |
end |
| 65 |
multiselect(_('関連付け'), :save_flag) do |
| 66 |
option :save, _('次回から、次の内容から始まるURLはこの方法で開く') do |
| 67 |
input nil, :save_uri |
| 68 |
end |
| 69 |
end |
| 70 |
end.next do |response| |
| 71 |
if response[:intent] |
| 72 |
Plugin::Intent::IntentToken.open( |
| 73 |
uri: uri, |
| 74 |
model: model, |
| 75 |
intent: response[:intent], |
| 76 |
parent: nil |
| 77 |
) |
| 78 |
if response[:save_flag].include?(:save) |
| 79 |
add_intent_rule( |
| 80 |
intent: response[:intent], |
| 81 |
str: response[:save_uri], |
| 82 |
rule: 'start', |
| 83 |
model_slug: specified_model_slug(model)) |
| 84 |
end |
| 85 |
end |
| 86 |
end |
| 87 |
end |
| 88 |
|
| 89 |
def add_intent_rule(intent:, str:, rule:, model_slug:) |
| 90 |
unless UserConfig[:intent_selector_rules].any?{|r| r[:intent].to_sym == intent.slug && r[:str] == str && r[:rule] == rule } |
| 91 |
UserConfig[:intent_selector_rules] += [{uuid: SecureRandom.uuid, intent: intent.slug, model: model_slug, str: str, rule: rule}] |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
ユーザが過去に入力したルールに基づき、 |
| 96 |
に分ける |
| 97 |
|
| 98 |
対象のintent |
| 99 |
ースのURI |
| 100 |
うModelのslug。 |
| 101 |
|
| 102 |
配列と、intentsに指定されたそれ以外の値の配列 |
| 103 |
def divide_intents(intents, uri, model_slug) |
| 104 |
intent_slugs = UserConfig[:intent_selector_rules].select{|record| |
| 105 |
((record[:model] == nil) || (model_slug == record[:model].to_s)) && uri.to_s.start_with?(record[:str]) |
| 106 |
}.map{|record| |
| 107 |
record[:intent].to_sym |
| 108 |
} |
| 109 |
intents.partition{|intent| intent_slugs.include?(intent.slug.to_sym) } |
| 110 |
end |
| 111 |
|
| 112 |
字列で得る。 |
| 113 |
|
| 114 |
タンス又はnil |
| 115 |
|
| 116 |
った場合は空文字列 |
| 117 |
def specified_model_slug(model) |
| 118 |
model ? model.class.slug.to_s : '' |
| 119 |
end |
| 120 |
|
| 121 |
end |