📄 wikipane.cs
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.* All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**/using System;
using System.Collections.Generic;
using System.Text;
using EmergeTk;
using EmergeTk.Model;
using System.Text.RegularExpressions;
using System.Drawing;
namespace EmergeTk.Widgets.Html
{
public delegate void WikiPageLoadHandler ( WikiPane pane, WikiPage newPage );
public class WikiPane : Pane
{
private Context context;
private ModelForm<WikiPage> form;
private WikiPage data;
private ImageButton editButton,backButton,forwardButton;
private Pane contextPane;
private List<WikiPage> history;
private int pageIndex = -1;
private string name;
public string Name
{
get { return name; }
set
{ if( name != value ) {
name = value;
setupPane(); }
}
} private bool receiveBookmark; public bool ReceiveBookmark { get { return receiveBookmark; } set { receiveBookmark = value; } }
float buttonDisabledOpacity = 0.2f;
private string defaultTarget;
public string DefaultTarget
{
get { return defaultTarget; }
set { defaultTarget = value; }
}
public override string ClientClass
{
get
{
return "Pane";
}
}
private void setupPane()
{
data = WikiPage.Load<WikiPage>(new FilterInfo("Name", name, FilterOperation.Equals));
if (data == null)
{
data = new WikiPage();
data.Name = name;
}
if (history != null && history.Count > pageIndex + 1)
{
history.RemoveRange(pageIndex + 1, history.Count - (pageIndex + 1));
forwardButton.Opacity = buttonDisabledOpacity;
}
if (data != null )
{
if (history == null)
history = new List<WikiPage>();
history.Add(data);
pageIndex++; if( ReceiveBookmark ) {
RootContext.AddFrameToHistory(
new ContextHistoryFrame(
new ContextHistoryHandler(goToPageIndex),
pageIndex, data.Name ) ); }
if (pageIndex > 0 && backButton != null ) backButton.Opacity = 1.0f;
}
drawContext();
}
void goToPageIndex(object state)
{
int index = (int)state;
if (index >= 0 && index < history.Count)
{
pageIndex = index;
data = history[pageIndex];
drawContext();
}
}
private void drawContext()
{
bool needToInit = false;
if (contextPane == null)
{
contextPane = RootContext.CreateWidget<Pane>();
contextPane.id = name;
contextPane.SetClientElementStyle("height", "'100%'");
if (Widgets != null && Widgets.Count > 0)
{
editButton.InsertBefore(contextPane);
}
else
{
Add(contextPane);
}
}
else if (context != null)
{
needToInit = true;
contextPane.InvokeClientMethod("FadeShow", "500");
context.ClearChildren();
}
string xml = data.ContextXml;
if (xml != null)
{
if (!xml.StartsWith("<Context>"))
xml = Util.SurroundTag(xml, "Context");
xml = wikizeInput(xml);
}
else
{
data.ContextXml = "==" + name + "==\r\n";
xml = string.Format("<Context>=={0}==No page here yet. Click on the edit icon below to start editing!</Context>",
name);
}
try
{
context = RootContext.CreateDynamicContext(name, xml);
contextPane.Add(context);
if (needToInit)
{
context.Init();
context.RecurseInit(context);
}
if (OnPageLoad != null)
OnPageLoad(this, data);
}
catch (Exception e)
{
Label error = RootContext.CreateWidget<Label>();
error.Text = string.Format(
@"Error occurred loading wiki page: \n\n
'''Message:''' {0}\n\n
'''StackTrace:''' {1}\n\n",e.Message,e.StackTrace.Replace("\n","\\n"));
error.ForeColor = "red";
contextPane.Add(error);
}
}
private string wikizeInput(string xml)
{
///support:
///!CamelCasing -> WikiLink (CamelCasing) (regex: ![A-Z][a-z]+[A-Z][a-z]+ )
///[[Main Page]] -> WikiLink (Main Page) (regex: \[\[(?<name>.*?)\]\]
///[[Main Page -> MainPane]] (Main Page in pane MainPane) \[\[(?<name>.*?)\s*->\s*(?<target>.*?)\]\]
///[[Main Page|index]] becomes index.
///[http://www.example.org link name] (resulting in "link name" as link)
//targeted
Regex targetedWikiLink = new Regex(@"!(\w+)->(\w+)", RegexOptions.Compiled);
Regex targetedMWikiLink = new Regex(@"\[\[(?<name>.*?)\s*?->\s*?(?<target>.*?)\]\]", RegexOptions.Compiled);
xml = targetedWikiLink.Replace(xml, new MatchEvaluator(delegate(Match m)
{ return string.Format("<WikiLink Name=\"{0}\" Target=\"{1}\"/>", m.Groups[1].Value.TrimStart('!'),m.Groups[2].Value); }));
xml = targetedMWikiLink.Replace(xml, new MatchEvaluator(delegate(Match m)
{ return string.Format("<WikiLink Name=\"{0}\" Target=\"{1}\"/>", m.Groups[1].Value.TrimStart('!'), m.Groups[2].Value); }));
//locals
Regex localWikiLink = new Regex(@"!(?<name>\w+)", RegexOptions.Compiled);
Regex localMWikiLink = new Regex(@"\[\[(?<name>.*?)(?<display>\|.*?)?\]\]", RegexOptions.Compiled);
xml = localWikiLink.Replace(xml, new MatchEvaluator(localLink));
xml = localMWikiLink.Replace(xml, new MatchEvaluator(localLink));
return xml;
}
private string localLink(Match m)
{
string name = m.Groups["name"].Value;
string text = m.Groups["display"] != null && m.Groups["display"].Success ? m.Groups["display"].Value.Trim('|') : name;
return string.Format("<WikiLink Name=\"{0}\" Label=\"{1}\"/>",name,text );
}
public override void Init()
{
editButton = RootContext.CreateWidget<ImageButton>();
editButton.Url = "images/edit.png";
editButton.ClassName = "wikiButton";
backButton = RootContext.CreateWidget<ImageButton>();
backButton.Url = "images/back.png";
backButton.ClassName = "wikiButton";
backButton.Opacity = buttonDisabledOpacity;
backButton.OnClick += new OnClickHandler(backButton_OnClick);
forwardButton = RootContext.CreateWidget<ImageButton>();
forwardButton.Url = "images/forward.png";
forwardButton.ClassName = "wikiButton";
forwardButton.Opacity = buttonDisabledOpacity;
forwardButton.OnClick += new OnClickHandler(forwardButton_OnClick);
editButton.OnClick += new OnClickHandler(editButton_OnClick);
Add(editButton, forwardButton, backButton);
if (RootContext.HttpContext.Request[this.UID] != null)
this.Name = RootContext.HttpContext.Request[this.UID]; if( receiveBookmark ) { RootContext.OnBookmark += new EventHandler(bookmarkCallback); }
}
void bookmarkCallback(object state, EventArgs e ) { string key = state as string; if( key != null ) { key = key.Trim('#'); Name = key; } }
void backButton_OnClick(Widget source, string args)
{
if (pageIndex > 0)
{
forwardButton.Opacity = 1.0f;
data = history[--pageIndex];
drawContext();
if (pageIndex == 0)
backButton.Opacity = buttonDisabledOpacity;
}
}
void forwardButton_OnClick(Widget source, string args)
{
if (pageIndex < history.Count-1)
{
data = history[++pageIndex];
drawContext();
backButton.Opacity = 1.0f;
if (pageIndex == history.Count - 1)
forwardButton.Opacity = buttonDisabledOpacity;
}
}
void editButton_OnClick(Widget source, string args)
{
if (data == null)
data = new WikiPage();
editButton.Visible = false;
if (context != null)
contextPane.Visible = false;
form = RootContext.CreateWidget<ModelForm<WikiPage>>();
form.DataBindWidget(data);
form.OnSubmit += new OnSubmitHandler<WikiPage>(form_OnSubmit);
form.OnCancel += new OnCancelHandler<WikiPage>(form_OnCancel);
form.Init();
Add(form);
}
void form_OnCancel(ModelForm<WikiPage> sender)
{
editButton.Visible = true;
if (context != null)
contextPane.Visible = true;
}
void form_OnSubmit(ModelForm<WikiPage> sender, object arg)
{
drawContext();
contextPane.Visible = true;
editButton.Visible = true;
form.Visible = false;
}
public event WikiPageLoadHandler OnPageLoad;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -