تطوير إضافات Discourse - الجزء الأول - إنشاء إضافة أساسية

إنشاء إضافة في Discourse يمكن أن يكون بسيطًا للغاية، بمجرد أن تتعلم بعض الخواص الغريبة. الهدف من هذا المنشور هو إنشاء هيكل عظمي للإضافة وتعريفك بالأساسيات.

بيئة التطوير الخاصة بك

تأكد من أن لديك بيئة تطوير Discourse تعمل على جهاز الكمبيوتر الخاص بك. أنصحك باستخدام دليل الإعداد المناسب والعودة هنا بعد الانتهاء.

plugin.rb

:tada: استخدم GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins · GitHub لإنشاء هيكل عظمي كامل لإضافة Discourse في دليل الإضافات الخاص بك :tada:

الهيكل العظمي الآن مدمج في نواة Discourse، وrake plugin:create[اسم-الإضافة] سينشئ إضافة باستخدام هذا الهيكل

عند بدء تشغيل Discourse، يبحث في دليل plugins عن أدلة فرعية تحتوي على ملف plugin.rb. ملف plugin.rb له غرضان: فهو manifesto للإضافة الخاصة بك مع المعلومات المطلوبة عنها بما في ذلك: اسمها، معلومات الاتصال ووصفها. الغرض الثاني هو تهيئة أي كود Ruby ضروري لتشغيل الإضافة.

في حالتنا، لن نضيف أي كود Ruby ولكننا لا زلنا بحاجة إلى plugin.rb. دعنا ننشئ الدليل basic-plugin مع ملف plugin.rb بداخله، بالمحتويات التالية:

basic-plugin/plugin.rb

# name: basic-plugin
# about: إضافة بسيطة للغاية لتوضيح كيفية عمل الإضافات
# version: 0.0.1
# authors: مطور إضافات رائع
# url: https://github.com/yourusername/basic-plugin

بمجرد إنشاء هذا الملف، يجب أن تعيد تشغيل خادمك المحلي وستتم تحميل الإضافة.

نقطة مهمة يجب الانتباه لها!

إذا كنت معتادًا على تطوير Rails العادي، قد تلاحظ أن الإضافات ليست لطيفة بنفس القدر عندما يتعلق الأمر بإعادة التحميل. بشكل عام، عند إجراء تغييرات على إضافتك، يجب عليك Ctrl+c لإيقاف تشغيل الخادم، ثم تشغيله مرة أخرى باستخدام bin/dev.

لم يتم التقاط تغييراتي! :warning:

أحيانًا لا يتم مسح ذاكرة التخزين المؤقت بالكامل، خاصة عند إنشاء ملفات جديدة أو حذف ملفات قديمة. للتغلب على هذه المشكلة، احذف مجلد tmp وأعد تشغيل Rails. على نظام Mac، يمكنك القيام بذلك في أمر واحد: rm -rf tmp; bin/dev.

التحقق من أن إضافتك تم تحميلها

بمجرد إعادة تشغيل خادمك المحلي، قم بزيارة الرابط /admin/plugins (تأكد من أنك مسجل الدخول كحساب مدير أولاً، لأن المديرين فقط يمكنهم رؤية سجل الإضافات).

إذا كل شيء عمل بشكل صحيح، يجب أن ترى إضافتك في القائمة:

تهانينا، لقد أنشأت إضافتك الأولى!

دعنا نضيف بعض JavaScript

حاليًا إضافتك لا تفعل أي شيء. دعنا نضيف ملف JavaScript سيظهر مربع تنبيه عند تحميل Discourse. هذا سيكون مزعجًا للغاية لأي مستخدم ولا يُنصح به كإضافة فعلية، لكنه سيوضح كيفية إدراج JavaScript في تطبيقنا قيد التشغيل.

أنشئ الملف التالي:

plugins/basic-plugin/assets/javascripts/discourse/initializers/alert.js

export default {
  name: "alert",
  initialize() {
    alert("مربعات التنبيه مزعجة!");
  },
};

الآن إذا أعدت تشغيل خادمك المحلي، يجب أن ترى “مربعات التنبيه مزعجة!” تظهر على الشاشة. (إذا لم يحدث ذلك، انظر إلى قسم “لم يتم التقاط تغييراتي” أعلاه).

دعنا نوضح كيف عمل هذا:

  1. ملفات JavaScript الموجودة في assets/javascripts/discourse/initializers يتم تنفيذها تلقائيًا عند تحميل تطبيق Discourse.

  2. هذا الملف بالتحديد exports كائن واحد، له name ووظيفة initialize.

  3. يجب أن يكون name فريدًا، لذا سميتها alert.

  4. يتم استدعاء وظيفة initialize() عند تحميل التطبيق. في حالتنا، كل ما تفعله هو تنفيذ كود alert() الخاص بنا.

أنت الآن مطور إضافات Discourse رسمي!


المزيد في السلسلة

الجزء 1: هذا الموضوع
الجزء 2: Plugin Outlets
الجزء 3: Site Settings
الجزء 4: git setup
الجزء 5: Admin interfaces
الجزء 6: Acceptance tests
الجزء 7: Publish your plugin


هذا المستند خاضع لإدارة الإصدارات - اقترح التغييرات على github.

110 إعجابات
Plugin API documentations?
How to create a new plugins?
Connecting to database with a plugin
Separating View from Data, Plugins, Modularity
Categories Topic Replies
How can I add "dislike" button?
Overwriting controllers in plugin
Create fully custom header
Added custom initializer
Which files can you override from a plugin?
Developing Discourse Plugins - Part 2 - Connect to a plugin outlet
Developing Discourse Plugins - Part 5 - Add an admin interface
(Superseded) Plugin Tutorial #1 - How to manipulate the text in the composer?
Which is Better? Discourse or Flarum?
Hide features for non-admin users through plugin
Customizing handlebars templates
How to properly display jalali dates for Persian language
Topic Ratings Plugin
Enable Like-Button for not logged-in visitors
Why do I need a block storage?
Custom Field not working
Rails plugin generator
Why GNU License?
Code reading help. ES6, plugin-outlet `_connectorCache`, custom-html `_customizations`, appEvents
Customizing handlebars templates
Application Files after Digital Ocean Setup
Application Files after Digital Ocean Setup
Application Files after Digital Ocean Setup
Make an external http request from discourse
Specify user by external id
How can I make my own Discourse plugins?
Reputation and level on member profile
How to Change User Profile Picture
Something like shortcodes for Discourse?
Calendar plugin features to make it really useful for us
Plugin Documentation Style Guide
Any options for over-riding the username restrictions?
How would updating effect custom overrides?
I would like to change the template completely
Adding a second tag description
Rails plugin generator
Creating a plugin
Fatal: Not a git repository (or any parent up to mount point /discourse)
Fatal: Not a git repository (or any parent up to mount point /discourse)
Adding command line tools support for user api keys
Discourse Code Modification for personal use
Plugin routing problems
[PAID] Mentorship needed
Can You Restrict Signups Using `swot` Email Verification?
Plugin API documentations?
Insert tag based upon word match
Triggering account creation/login on external service when a user logs in on discourse
Place for total coding beginners to learn how to customise?
Add additional button post menu
Fetch third party data for topic list
The Ignore User feature (now in 2.3.0.beta7)
Help with a plugin
Create Custom APIs
Proper Development and Deployment
How to make only a group of users can send messages to staff
How might we better structure #howto?
Theme-Component v Plugin: What's the difference
Creating Parsing Extensions
How can I customized my own site
All latest images in posts from a category
(Ruby) Selecting only posts that are made by non-original posters
Hosting discourse on our infrastructure AWS
Display FullName instead of UserName in the Profile
Calling Python scripts w/ arguments on the backend via plugin?
Extend Existing Controller?
How to add custom fields to models
Extend Existing Controller?
Bot writer's tip: Processing every post
Add user to group after purchase
Support for proposed plug-in
Autocompletion for a user field at signup
Custom discourse with limited features
Overriding user_guardian.rb in a plugin (no fork necessary!)
Problems with git clone
Friendly user profile url
Website Integration
Discourse developement environment setup
How to show Full Name in Embeds
How to display full name in comments embeds
Rake task "assets:precompile" is failing due to JS Compilation issue
Change code does not work in VS Studio code editor
Edit Code
Discourse Chain Topics Plugin
Contributing to Discourse
How to edit topics/show.html.erb
Install Discourse on macOS for development
Developing Discourse Plugins - Part 3 - Add custom site settings
Developing Discourse Plugins - Part 4 - Setup git
Developing Discourse Plugins - Part 7 - Publish your plugin
Developing Discourse Plugins - Part 6 - Add acceptance tests
Learn how to start building stuff for Discourse if you're newbie (like myself)
How to disable updating username for admins & moderators?
Backend code
Backend code
Install Discourse on Ubuntu or Debian for Development
How to make changes on backend side of discourse application using rails
Introduction to Discourse Development
Discourse blog (article and comments)
How to learn more about Discourse back-end?
I want to learn how to program for Discourse
Discourse Audio Message
Create custom Automations
How can one add a toggle button to the post menu and implement an action for that toggle button in a Discourse plugin?
Content box
Create an advertising page
Discourse 有自定义插件模板吗,如何写自定义插件呢
Deploy custom Discourse repository via docker
Multiple custom fields for topics similar to custom userfields
How do I go about making a very customized theme?
How can I create a Discourse that's just like Stemaway.com?
Adding new columns to user lists in groups
How to pin a post within a topic?
Ruby script to send automatic welcome message to new users
:cn: Installing the Discourse development environment on Ubuntu 在 Ubuntu 上安装 Discourse 开发环境
Admin not accessible because of auto-minify for Cloudflare
Store pdf and doc files as raw text in the database - Where to start?
Update Password Encryption Method
I'm trying to run discourse app locally in production env without docker
How to turn off "Body seems unclear, is it a complete sentence?'
Hosting dynamic pages with discourse
Invisible way to force display order of tags?
Controllers and Routes
Learn about Discourse client-side flow?
How do you learn to build Discourse plugins?
How do you learn to build Discourse plugins?
Admin not accessible because of auto-minify for Cloudflare
Customizing CSS via plugin
About the Plugin category
Visual diagraming to add visual dimension to Conversations?
Custom Onebox engine
How can I install my own plugin written in Ruby?
Allow reply-to individual instead of topic/forum (mailing list feature)
Adapt github changes to my own site
Push to digital ocean from command line and rebuild
How to convince my university to use/install Discourse?
Hack to enable invitations for Trust level 1 users
Dropcaps in Discourse - cannot override span tag
Getting certain posts to Zapier
Plugin: add a menu icon (next to search)
Error When creating custom plugin
Error When creating custom plugin
IOTA as a currency for Discourse
New columns in directory (/users)
Bootstrap fails when plugin file specifies gem version range
Running my own discourse image
How to add a job to cron/anacron when creating Docker container
How to transfer data from plugin to app/views templates?