Skip to content

Commit

Permalink
Hide edit buttons if not logged in for Classic
Browse files Browse the repository at this point in the history
Inject a shadow tiddler with content set to either 'yes' or 'no',
then look for that shadow tiddler in the upload plugin.

WIP, but it's generally working.
  • Loading branch information
simonbaird committed Feb 27, 2024
1 parent 5ea426a commit 3225c84
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
47 changes: 26 additions & 21 deletions rails/lib/th_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ def strip_external_core_url_prefix
self
end

def apply_tw5_mods(site_name, for_download: false, local_core: false, use_put_saver: false, is_logged_in: false)
# Provide a way for TiddlyWikis to detect when they're being viewed by their
# owner. The readonly plugin might user this to hide tiddler edit buttons, etc.
def status_is_logged_in(for_download: false, is_logged_in: false)
if !for_download && is_logged_in
'yes'
else
'no'
end
end

def apply_tw5_mods(site_name, for_download:, local_core:, use_put_saver:, is_logged_in:)
upload_url = if for_download || use_put_saver
# Clear $:/UploadURL for downloads so the save button in the downloaded
# file will not try to use upload.js. It should use another save
Expand All @@ -72,17 +82,6 @@ def apply_tw5_mods(site_name, for_download: false, local_core: false, use_put_sa
Settings.subdomain_site_url(site_name)
end

if !for_download && is_logged_in
# Provide a way for TiddlyWiki files to detect when
# they're being viewed by their owner
status_is_logged_in = 'yes'

else
# The readonly plugin might user this to hide tiddler edit buttons, etc.
status_is_logged_in = 'no'

end

write_tiddlers({
# TiddlyWiki will POST to this url using code in core/modules/savers/upload.js
'$:/UploadURL' => upload_url,
Expand All @@ -93,7 +92,7 @@ def apply_tw5_mods(site_name, for_download: false, local_core: false, use_put_sa
'$:/UploadWithUrlOnly' => 'yes',

# Provide a way for TiddlyWikis to detect when they're able to be saved
'$:/status/IsLoggedIn' => status_is_logged_in,
'$:/status/IsLoggedIn' => status_is_logged_in(for_download:, is_logged_in:)
})

# Since every save uploads the entire TiddlyWiki I want to discourage
Expand All @@ -115,7 +114,7 @@ def apply_tw5_mods(site_name, for_download: false, local_core: false, use_put_sa
end
end

def apply_classic_mods(site_name)
def apply_classic_mods(site_name, for_download:, is_logged_in:)
# We don't want to hard code the site url in the plugin, but we also don't
# want to hard code the domain name and port etc since they're different
# in different environments. This is clever way to deal with that.
Expand All @@ -129,24 +128,30 @@ def apply_classic_mods(site_name)
}
})

# This could be a regular tiddler, but let's make it a shadow tiddler just to be cool.
# Will be clickable when viewing the plugin since we used 'TiddlyHost' as the modifier above.
# (I'm using camel case intentionally here despite the usual spelling of Tiddlyhost.)
write_shadow_tiddlers({
# This could be a regular tiddler, but let's make it a shadow tiddler just to be cool.
# Will be clickable when viewing the plugin since we used 'TiddlyHost' as the modifier above.
# (I'm using camel case intentionally here despite the usual spelling of Tiddlyhost.)
'TiddlyHost' => {
text: "[[Tiddlyhost|#{Settings.main_site_url}]] is a hosting service for ~TiddlyWiki.",
modifier: 'TiddlyHost',
}
},

# If we set this to 'yes' then the ThostUploadPlugin will decide to show the TiddlyWiki
# in readonly mode, i.e. without the new tiddler or edit buttons showing
'TiddlyHostIsLoggedIn' => {
text: status_is_logged_in(for_download:, is_logged_in:),
modifier: 'TiddlyHost',
},
})
end

def apply_tiddlyhost_mods(site_name, for_download: false, local_core: false, use_put_saver: false, is_logged_in: false)
if is_tw5?
apply_tw5_mods(site_name,
for_download: for_download, local_core: local_core, use_put_saver: use_put_saver, is_logged_in: is_logged_in)
apply_tw5_mods(site_name, for_download:, local_core:, use_put_saver:, is_logged_in:)

elsif is_classic?
apply_classic_mods(site_name)
apply_classic_mods(site_name, for_download:, is_logged_in:)

else # FeatherWiki
# No hackery for FeatherWiki currently
Expand Down
18 changes: 10 additions & 8 deletions rails/tw_content/plugins/thost_upload_plugin.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ bidix.initOption('txtThostSiteName','<%= site_name %>');
// Tiddlyhost stuff
//

// So you can see edit controls via http
config.options.chkHttpReadOnly = false;
window.readOnly = false;
window.showBackstage = true;

// Add 'upload to tiddlyhost' button
config.shadowTiddlers.SideBarOptions = config.shadowTiddlers.SideBarOptions
.replace(/(<<saveChanges>>)/,"$1<<thostUpload>>");
bidix.actReadOnly = config.shadowTiddlers.TiddlyHostIsLoggedIn == "no"
config.options.chkHttpReadOnly = bidix.actReadOnly;
window.readOnly = bidix.actReadOnly;
window.showBackstage = !bidix.actReadOnly;

if (!bidix.actReadOnly) {
// Add 'upload to tiddlyhost' button
config.shadowTiddlers.SideBarOptions = config.shadowTiddlers.SideBarOptions
.replace(/(<<saveChanges>>)/,"$1<<thostUpload>>");
}

//}}}

0 comments on commit 3225c84

Please sign in to comment.