Skip to content

Commit

Permalink
add preview function
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Nov 19, 2017
1 parent b8788c9 commit ab180aa
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
19 changes: 19 additions & 0 deletions admin/preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

include 'common.php';

/** 获取内容 Widget */
Typecho_Widget::widget('Widget_Archive', 'type=single&checkPermalink=0&preview=1')->to($content);

/** 检测是否存在 */
if (!$content->have()) {
$response->redirect($options->adminUrl);
}

/** 检测权限 */
if (!$user->pass('editor', true) && $content->authorId != $user->uid) {
$response->redirect($options->adminUrl);
}

/** 输出内容 */
$content->render();
26 changes: 21 additions & 5 deletions admin/write-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ function justifySlugWidth() {

var submitted = false, form = $('form[name=write_post],form[name=write_page]').submit(function () {
submitted = true;
}), savedData = null;
}), savedData = null,
formAction = form.attr('action'),
idInput = $('input[name=cid]'),
cid = idInput.val(),
draft = $('input[name=draft]'),
draftId = draft.length > 0 ? draft.val() : 0,
btnSave = $('#btn-save');

// 计算夏令时偏移
var dstOffset = (function () {
Expand All @@ -172,28 +178,27 @@ function justifySlugWidth() {
// 自动保存
<?php if ($options->autoSave): ?>
var locked = false,
formAction = form.attr('action'),
idInput = $('input[name=cid]'),
cid = idInput.val(),
autoSave = $('<span id="auto-save-message" class="left"></span>').prependTo('.submit'),
autoSaveOnce = !!cid,
lastSaveTime = null;

function autoSaveListener () {
setInterval(function () {
idInput.val(cid);
var data = form.serialize();

if (savedData != data && !locked) {
locked = true;
btnSave.attr('disabled', 'disabled');

autoSave.text('<?php _e('正在保存'); ?>');
$.post(formAction, data + '&do=save', function (o) {
savedData = data;
lastSaveTime = o.time;
cid = o.cid;
idInput.val(cid);
autoSave.text('<?php _e('已保存'); ?>' + ' (' + o.time + ')').effect('highlight', 1000);
locked = false;
btnSave.removeAttr('disabled');
}, 'json');
}
}, 10000);
Expand Down Expand Up @@ -229,6 +234,17 @@ function autoSaveListener () {
}
});

// 预览功能
$('#btn-preview').click(function () {
var data = form.serialize();

if (lastData != data) {
if (confirm('<?php _e('内容已经改变尚未保存, 需要保存后才能预览, 是否保存?'); ?>')) {

}
}
});

// 控制选项和附件的切换
var fileUploadInit = false;
$('#edit-secondary .typecho-option-tabs li').click(function() {
Expand Down
13 changes: 9 additions & 4 deletions admin/write-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
<div class="row typecho-page-main typecho-post-area" role="form">
<form action="<?php $security->index('/action/contents-page-edit'); ?>" method="post" name="write_page">
<div class="col-mb-12 col-tb-9" role="main">
<?php if ($page->draft && $page->draft['cid'] != $page->cid): ?>
<?php $pageModifyDate = new Typecho_Date($page->draft['modified']); ?>
<cite class="edit-draft-notice"><?php _e('当前正在编辑的是保存于%s的草稿, 你可以<a href="%s">删除它</a>', $pageModifyDate->word(),
$security->getIndex('/action/contents-page-edit?do=deleteDraft&cid=' . $page->cid)); ?></cite>
<?php if ($page->draft): ?>
<?php if ($page->draft['cid'] != $page->cid): ?>
<?php $pageModifyDate = new Typecho_Date($page->draft['modified']); ?>
<cite class="edit-draft-notice"><?php _e('当前正在编辑的是保存于%s的草稿, 你可以<a href="%s">删除它</a>', $pageModifyDate->word(),
$security->getIndex('/action/contents-page-edit?do=deleteDraft&cid=' . $page->cid)); ?></cite>
<?php else: ?>
<cite class="edit-draft-notice"><?php _e('当前正在编辑的是未发布的草稿'); ?></cite>
<?php endif; ?>
<input name="draft" type="hidden" value="<?php echo $page->draft['cid'] ?>" />
<?php endif; ?>

<p class="title">
Expand Down
13 changes: 9 additions & 4 deletions admin/write-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
<div class="row typecho-page-main typecho-post-area" role="form">
<form action="<?php $security->index('/action/contents-post-edit'); ?>" method="post" name="write_post">
<div class="col-mb-12 col-tb-9" role="main">
<?php if ($post->draft && $post->draft['cid'] != $post->cid): ?>
<?php $postModifyDate = new Typecho_Date($post->draft['modified']); ?>
<cite class="edit-draft-notice"><?php _e('你正在编辑的是保存于 %s 的草稿, 你也可以 <a href="%s">删除它</a>', $postModifyDate->word(),
$security->getIndex('/action/contents-post-edit?do=deleteDraft&cid=' . $post->cid)); ?></cite>
<?php if ($post->draft): ?>
<?php if ($post->draft['cid'] != $post->cid): ?>
<?php $postModifyDate = new Typecho_Date($post->draft['modified']); ?>
<cite class="edit-draft-notice"><?php _e('你正在编辑的是保存于 %s 的草稿, 你也可以 <a href="%s">删除它</a>', $postModifyDate->word(),
$security->getIndex('/action/contents-post-edit?do=deleteDraft&cid=' . $post->cid)); ?></cite>
<?php else: ?>
<cite class="edit-draft-notice"><?php _e('当前正在编辑的是未发布的草稿'); ?></cite>
<?php endif; ?>
<input name="draft" type="hidden" value="<?php echo $post->draft['cid'] ?>" />
<?php endif; ?>

<p class="title">
Expand Down
47 changes: 26 additions & 21 deletions var/Widget/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ public function __construct($request, $response, $params = NULL)
$this->parameter->setDefault(array(
'pageSize' => $this->options->pageSize,
'type' => NULL,
'checkPermalink' => true
'checkPermalink' => true,
'preview' => false
));

/** 用于判断是路由调用还是外部调用 */
Expand Down Expand Up @@ -775,12 +776,12 @@ private function singleHandle(Typecho_Db_Query $select, &$hasPushed)
}

/** 匹配缩略名 */
if (isset($this->request->slug)) {
if (isset($this->request->slug) && !$this->parameter->preview) {
$select->where('table.contents.slug = ?', $this->request->slug);
}

/** 匹配时间 */
if (isset($this->request->year)) {
if (isset($this->request->year) && !$this->parameter->preview) {
$year = $this->request->filter('int')->year;

$fromMonth = 1;
Expand Down Expand Up @@ -809,7 +810,7 @@ private function singleHandle(Typecho_Db_Query $select, &$hasPushed)
}

/** 保存密码至cookie */
if ($this->request->isPost() && isset($this->request->protectPassword)) {
if ($this->request->isPost() && isset($this->request->protectPassword) && !$this->parameter->preview) {
$this->security->protect();
Typecho_Cookie::set('protectPassword', $this->request->protectPassword, 0);
}
Expand All @@ -819,8 +820,8 @@ private function singleHandle(Typecho_Db_Query $select, &$hasPushed)
$this->query($select);

if (!$this->have()
|| (isset($this->request->category) && $this->category != $this->request->category)
|| (isset($this->request->directory) && $this->request->directory != implode('/', $this->directory))) {
|| (isset($this->request->category) && $this->category != $this->request->category && !$this->parameter->preview)
|| (isset($this->request->directory) && $this->request->directory != implode('/', $this->directory) && !$this->parameter->preview)) {
if (!$this->_invokeFromOutside) {
/** 对没有索引情况下的判断 */
throw new Typecho_Widget_Exception(_t('请求的地址不存在'), 404);
Expand Down Expand Up @@ -860,7 +861,7 @@ private function singleHandle(Typecho_Db_Query $select, &$hasPushed)
}

/** 设置归档类型 */
$this->_archiveType = $this->type;
list($this->_archiveType) = explode('_', $this->type);

/** 设置归档缩略名 */
$this->_archiveSlug = ('post' == $this->type || 'attachment' == $this->type) ? $this->cid : $this->slug;
Expand Down Expand Up @@ -1333,24 +1334,28 @@ public function execute()

/** 定时发布功能 */
if (!$selectPlugged) {
if ('post' == $this->parameter->type || 'page' == $this->parameter->type) {
if ($this->user->hasLogin()) {
$select = $this->select()->where('table.contents.status = ? OR table.contents.status = ? OR
if ($this->parameter->preview) {
$select = $this->select();
} else {
if ('post' == $this->parameter->type || 'page' == $this->parameter->type) {
if ($this->user->hasLogin()) {
$select = $this->select()->where('table.contents.status = ? OR table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)',
'publish', 'hidden', 'private', $this->user->uid);
} else {
$select = $this->select()->where('table.contents.status = ? OR table.contents.status = ?',
} else {
$select = $this->select()->where('table.contents.status = ? OR table.contents.status = ?',
'publish', 'hidden');
}
} else {
if ($this->user->hasLogin()) {
$select = $this->select()->where('table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)', 'publish', 'private', $this->user->uid);
}
} else {
$select = $this->select()->where('table.contents.status = ?', 'publish');
if ($this->user->hasLogin()) {
$select = $this->select()->where('table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)', 'publish', 'private', $this->user->uid);
} else {
$select = $this->select()->where('table.contents.status = ?', 'publish');
}
}
$select->where('table.contents.created < ?', $this->options->time);
}
$select->where('table.contents.created < ?', $this->options->time);
}

/** handle初始化 */
Expand All @@ -1361,15 +1366,15 @@ public function execute()
$this->_feedRssUrl = $this->options->feedRssUrl;
$this->_feedAtomUrl = $this->options->feedAtomUrl;
$this->_keywords = $this->options->keywords;
$this->_description = $this->options->description;
$this->_description = $this->options->description;

if (isset($handles[$this->parameter->type])) {
$handle = $handles[$this->parameter->type];
$this->{$handle}($select, $hasPushed);
} else {
$hasPushed = $this->pluginHandle()->handle($this->parameter->type, $this, $select);
}

/** 初始化皮肤函数 */
$functionsFile = $this->_themeDir . 'functions.php';
if ((!$this->_invokeFromOutside || $this->parameter->type == 404) && file_exists($functionsFile)) {
Expand Down

0 comments on commit ab180aa

Please sign in to comment.